我试图将数据框中的每一列从第一列转换为数字类型,我尝试过:
frames<-lapply(frames,function(x) {x[,2:ncol(x)]<-as.numeric(as.character(x[,2:ncol(x)]))} )
然而,这是用NAs取代所有东西
还试过
frames<-lapply(frames,function(x) {x[,2:ncol(x)]<-as.numeric(as.character(unlist(x[,2:ncol(x)])) ) } )
然而,这种尝试并没有保留帧的结构
创建等效数据集的代码:
test<-data.frame(Col1=c(1,2,3,4), Col2=c(2,4,5,5) , Col3=c(245,51,23,5) )
test2<-data.frame(Col1= c(1,3,1,4), Col2= c(2,3,5,6) ,Col3= c(2,51,23,5))
test[,2:3]<- lapply(test[,2:3], as.factor)
test2[,2:3]<- lapply(test2[,2:3], as.factor)
test_list<-list(test,test2)
现在两个帧都在test_list中,我想将每个测试数据帧的第二和第三列转换为数字类型。
dput(frames[[1]][1:5,1:5])
structure(list(date = c("04/03/13 - 10/03/13", "11/03/13 - 17/03/13",
"18/03/13 - 24/03/13", "25/03/13 - 31/03/13", "01/04/13 - 07/04/13" ),
`Summary Z: ` = c("12", "9", "12", "10", "13"), `Summary O: ` = c("24",
"28", "24", "15", "17"), `Summary M: ` = c("3", "4", "3", "2", "1"),
`Summary S: ` = c("2", "2", "6", "3", "4")), .Names = c("date", "Summary Z: ",
"Summary O: ", "Summary M: ", "Summary S: " ), row.names = 7:11,
class = "data.frame")
答案 0 :(得分:2)
您可以在lapply
中使用list
将factor
列转换为numeric
。在提供的示例中,first
列除外,所有其他列都需要转换为numeric
。在下面的代码中,通过x[,-1]
取消选择第1列。通过使用lapply(x,[,-1],..)
,它会将这些列转换为列表中的numeric
。将<-
分配给x[,-1]
可恢复已转换列的原始data.frame
结构。在最后一步中,return(x)
或仅x
将返回列表中的x
data.frame。
res <- lapply(frames, function(x) {
x[,-1] <- lapply(x[,-1], function(.x) as.numeric(as.character(.x)))
x})
lapply(res,head,2)
# [[1]]
# V1 V2 V3 V4 V5 V6
#1 N 10 19 15 8 7
#2 O 15 3 17 9 7
# [[2]]
# V1 V2 V3 V4 V5 V6
#1 K 1 12 14 13 13
#2 O 3 4 1 5 17
使用test_list
数据集
res2 <- lapply(test_list, function(x) {
x[,-1] <- lapply(x[,-1], function(.x) as.numeric(as.character(.x)))
x})
str(res2)
# List of 2
#$ :'data.frame': 4 obs. of 3 variables:
# ..$ Col1: num [1:4] 1 2 3 4
#..$ Col2: num [1:4] 2 4 5 5
#..$ Col3: num [1:4] 245 51 23 5
#$ :'data.frame': 4 obs. of 3 variables:
#..$ Col1: num [1:4] 1 3 1 4
#..$ Col2: num [1:4] 2 3 5 6
#..$ Col3: num [1:4] 2 51 23 5
使用dput
输出
frames <- structure(list(date = c("04/03/13 - 10/03/13", "11/03/13 - 17/03/13",
"18/03/13 - 24/03/13", "25/03/13 - 31/03/13", "01/04/13 - 07/04/13" ),
`Summary Z: ` = c("12", "9", "12", "10", "13"), `Summary O: ` = c("24",
"28", "24", "15", "17"), `Summary M: ` = c("3", "4", "3", "2", "1"),
`Summary S: ` = c("2", "2", "6", "3", "4")), .Names = c("date", "Summary Z: ",
"Summary O: ", "Summary M: ", "Summary S: " ), row.names = 7:11,
class = "data.frame")
frameslist <- list(frames, frames)
frameslist <- lapply(frameslist, function(x) {
x[,-1] <- lapply(x[,-1], as.numeric)
x})
str(frameslist[[1]])
#'data.frame': 5 obs. of 5 variables:
#$ date : chr "04/03/13 - 10/03/13" "11/03/13 - 17/03/13" "18/03/13 - 24/03/13" "25/03/13 - 31/03/13" ...
# $ Summary Z: : num 12 9 12 10 13
# $ Summary O: : num 24 28 24 15 17
# $ Summary M: : num 3 4 3 2 1
# $ Summary S: : num 2 2 6 3 4