R将列类从数据框分配(或复制)到另一个

时间:2014-12-08 15:18:44

标签: r class dataframe copy assign

我制作了一个大型数据框(1700 + obs,159个变量),其功能是从网站收集信息。通常,函数会查找某些列的数值,因此它们是数字的。但是,有时它会找到一些文本,并将整个列转换为文本。 我有一个df,其列类是正确的,我想"粘贴"那些类到一个新的,不正确的df。 比方说,例如:

dfCorrect<-data.frame(x=c(1,2,3,4),y=as.factor(c("a","b","c","d")),z=c("bar","foo","dat","dot"),stringsAsFactors = F)
str(dfCorrect)
'data.frame':   4 obs. of  3 variables:
 $ x: num  1 2 3 4
 $ y: Factor w/ 4 levels "a","b","c","d": 1 2 3 4
 $ z: chr  "bar" "foo" "dat" "dot"

## now I have my "wrong" data frame:
dfWrong<-as.data.frame(sapply(dfCorrect,paste,sep=""))
str(dfWrong)
'data.frame':   4 obs. of  3 variables:
 $ x: Factor w/ 4 levels "1","2","3","4": 1 2 3 4
 $ y: Factor w/ 4 levels "a","b","c","d": 1 2 3 4
 $ z: Factor w/ 4 levels "bar","dat","dot",..: 1 4 2 3

我想将dfCorrect的每一列的类复制到dfWrong,但尚未找到如何正确执行此操作。 我已经测试过:

dfWrong1<-dfWrong
dfWrong1[0,]<-dfCorrect[0,]
str(dfWrong1) ## bad result
'data.frame':   4 obs. of  3 variables:
 $ x: Factor w/ 4 levels "1","2","3","4": 1 2 3 4
 $ y: Factor w/ 4 levels "a","b","c","d": 1 2 3 4
 $ z: Factor w/ 4 levels "bar","dat","dot",..: 1 4 2 3

dfWrong1<-dfWrong
str(dfWrong1)<-str(dfCorrect)
'data.frame':   4 obs. of  3 variables:
 $ x: num  1 2 3 4
 $ y: Factor w/ 4 levels "a","b","c","d": 1 2 3 4
 $ z: chr  "bar" "foo" "dat" "dot"
Error in str(dfWrong1) <- str(dfCorrect) : 
  could not find function "str<-"

有了这个小矩阵,我可以手工操作,但是较大的那个呢?有没有办法去复制&#34;从一个df到另一个的类,而不必知道每列的各个类(和索引)?

预期的最终结果(在正确&#34;粘贴&#34;类之后):

all.equal(sapply(dfCorrect,class),sapply(dfWrong,class))
[1] TRUE

谢谢,

1 个答案:

答案 0 :(得分:7)

你可以试试这个:

dfWrong[] <- mapply(FUN = as,dfWrong,sapply(dfCorrect,class),SIMPLIFY = FALSE)

......虽然我的第一直觉就是同意奥利弗的意见,如果是我,我会尽量确保你正在阅读数据时正确上课。