All of my data comes in character format. When I try transforming a subset of the data in to numeric using apply
it doesn't seem to work.
我的所有数据都以字符格式显示。当我尝试使用apply将数据的子集转换为数字时,它似乎不起作用。
df2 <- as.data.frame(matrix(as.character(1:9),3,3))
df2[,-2] <- apply(df2[,-2], 2, as.numeric)
apply(df2, 2, class)
Could somebody point me out what I am doing wrong in the example above? Thanks
有人能指出我在上面的例子中做错了什么吗?谢谢
1 个解决方案
#1
1
As commented above.. a matrix
in R can only hold values of the same type in all columns. You cannot change some of the values to numeric and leave some others as characters. If you want different data types, you can use a data.frame
, but even then, you can only have one data type per column.
如上所述...... R中的矩阵只能在所有列中保存相同类型的值。您不能将某些值更改为数字,而将其他值更改为字符。如果您需要不同的数据类型,可以使用data.frame,但即便如此,每列只能有一种数据类型。
For your example case:
对于您的示例案例:
df2 <- as.data.frame(matrix(as.character(1:9),3,3))
will create a data.frame with factor
s in each column. If you want to convert the second column to numeric
, you can do:
将创建一个data.frame,其中包含每列中的因子。如果要将第二列转换为数字,则可以执行以下操作:
df2$V2 <- as.numeric(levels(df2$V2))[df2$V2]
Or
要么
df$V2 <- as.numeric(as.character(df2$V2))
So you don't need to use apply
in this case.
所以在这种情况下你不需要使用apply。
str(df2)
#'data.frame': 3 obs. of 3 variables:
# $ V1: Factor w/ 3 levels "1","2","3": 1 2 3
# $ V2: num 4 5 6
# $ V3: Factor w/ 3 levels "7","8","9": 1 2 3
If you wanted to convert all columns to numeric, you can do:
如果要将所有列转换为数字,可以执行以下操作:
# if the columns were factors before:
df2[] <- lapply(df2, function(i) as.numeric(levels(i))[i])
Or
要么
# if the columns were characters before:
df2[] <- lapply(df2, as.numeric)
#1
1
As commented above.. a matrix
in R can only hold values of the same type in all columns. You cannot change some of the values to numeric and leave some others as characters. If you want different data types, you can use a data.frame
, but even then, you can only have one data type per column.
如上所述...... R中的矩阵只能在所有列中保存相同类型的值。您不能将某些值更改为数字,而将其他值更改为字符。如果您需要不同的数据类型,可以使用data.frame,但即便如此,每列只能有一种数据类型。
For your example case:
对于您的示例案例:
df2 <- as.data.frame(matrix(as.character(1:9),3,3))
will create a data.frame with factor
s in each column. If you want to convert the second column to numeric
, you can do:
将创建一个data.frame,其中包含每列中的因子。如果要将第二列转换为数字,则可以执行以下操作:
df2$V2 <- as.numeric(levels(df2$V2))[df2$V2]
Or
要么
df$V2 <- as.numeric(as.character(df2$V2))
So you don't need to use apply
in this case.
所以在这种情况下你不需要使用apply。
str(df2)
#'data.frame': 3 obs. of 3 variables:
# $ V1: Factor w/ 3 levels "1","2","3": 1 2 3
# $ V2: num 4 5 6
# $ V3: Factor w/ 3 levels "7","8","9": 1 2 3
If you wanted to convert all columns to numeric, you can do:
如果要将所有列转换为数字,可以执行以下操作:
# if the columns were factors before:
df2[] <- lapply(df2, function(i) as.numeric(levels(i))[i])
Or
要么
# if the columns were characters before:
df2[] <- lapply(df2, as.numeric)