[英]R - Why does a data.frame turn columns consisting only of numbers into a character vector?
Here is a simple dataframe I made, the data is originally in a pdf which is why I entered the column names first and then the rows like so.这是我制作的一个简单的 dataframe,数据最初在 pdf 中,这就是为什么我先输入列名,然后输入行。
skupni_indeksi <- data.frame(Proizvod =NA, Mjerna_jedinica=NA,Količine_2007=0,Količine_2008=0,Cijene_2007=0,Cijene_2008=0,stringsAsFactors = F)
skupni_indeksi[1,] <- c("A", "komad", 10, 12, 25, 30)
skupni_indeksi[2,] <-c("B", "litra", 30, 40, 40, 40)
skupni_indeksi[3,] <-c("C", "m2", 20, 15, 60, 72)
I know there are other ways of inputting such data, but my question concerns the behaviour of dataframes.我知道还有其他输入此类数据的方法,但我的问题涉及数据框的行为。 Look at the column Cijene_2007, for example.
例如,查看 Cijene_2007 列。 It is originally a single number, and the new rows all place numbers into that column.
它最初是一个数字,新行都将数字放入该列。 Why is the column of a character type, when checking with str(skupni_indeksi)?
为什么使用 str(skupni_indeksi) 检查时是字符类型的列?
I found a manual solution to turn desired columns into numeric, simply:我找到了一种手动解决方案,可以将所需的列转换为数字,简单地说:
skupni_indeksi[,3:6] <- sapply(skupni_indeksi[,3:6], as.numeric)
which fixes the issue.这解决了这个问题。 Could I have done something different in the first place to not need this line?
我可以先做一些不同的事情而不需要这条线吗?
c("A", "komad", 10, 12, 25, 30)
is a vector. c("A", "komad", 10, 12, 25, 30)
是一个向量。 A vector can have only one class.一个向量只能有一个 class。
Since you have numbers and characters mixed it turns the number to characters.由于您混合了数字和字符,因此它将数字转换为字符。
class(c("A", "komad", 10, 12, 25, 30))
#[1] "character"
You are filling the dataframe in a row-wise fashion.您正在以逐行方式填充 dataframe。 Usually, it is better to fill them in column-wise so that such data conversion do not take place.
通常,最好按列填充它们,这样就不会发生这种数据转换。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.