[英]Data frame not getting converted to numeric
I have 100 files in a list which I would access using: 我在列表中有100个文件,可以使用以下文件进行访问:
my.data[[1]], my.data[[2]] ... my.data[[100]]
each of these is a data.frame containing 202 columns and 2080 rows and I would like to convert them to numeric class. 这些都是data.frame,包含202列和2080行,我想将它们转换为数字类。 I tried with: 我尝试过:
for (i in 1:100) {
my.data[[i]] <- sapply(my.data[[i]], as.numeric)
}
but when I check the class after running this loop using: 但是当我使用以下命令运行此循环后检查类时:
class(my.data[[1]])
I get the class as "matrix". 我把这门课称为“矩阵”。
Of course, I tried to check them with one of the data.frames before looping; 当然,我尝试在循环之前用data.frames之一检查它们; that too gave me the same result. 那也给了我同样的结果。
Please help. 请帮忙。 Where I could possibly be wrong? 我可能在哪里错了?
Try: 尝试:
set.seed(49)
my.data <- lapply(1:5, function(x) as.data.frame(matrix(as.character(sample(20,5*6,replace=TRUE)), ncol=6),stringsAsFactors=F))
my.data[] <- lapply(my.data, function(x) {x[] <-lapply(x, as.numeric);x})
str(my.data)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.