R将列类从一个数据帧分配(或复制)到另一个 [英] R Assign (or copy) column classes from a data frame to another
问题描述
我制作了一个大型数据框(1700 + obs,159个变量),并带有从网站收集信息的功能.通常,该函数查找某些列的数字值,因此它们是数字.但是,有时它会找到一些文本,然后将整个列转换为文本. 我有一个df,其列类正确,并且我想将这些类粘贴"到新的,不正确的df中. 举例来说:
I produced a large data frame (1700+obs,159 variables) with a function that collects info from a website. Usually, the function finds numeric values for some columns, and thus they're numeric. Sometimes, however, it finds some text, and converts the whole column to text. I have one df whose column classes are correct, and I would like to "paste" those classes to a new, incorrect df. Say, for example:
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
,但是还没有找到如何正确执行的方法.
我已经测试过:
I wanted to copy the classes of each column of dfCorrect
into dfWrong
, but haven't found how to do it properly.
I've tested:
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<-"
有了这个小的矩阵,我可以手工处理,但是较大的矩阵呢?有没有一种方法可以将类从一个df复制到另一个df,而不必知道每一列的各个类(和索引)?
With this small matrix I could go by hand, but what about larger ones? Is there a way to "copy" the classes from one df to another without having to know the individual classes (and indexes) of each column?
预期的最终结果(在正确地粘贴"类之后):
Expected final result (after properly "pasting" classes):
all.equal(sapply(dfCorrect,class),sapply(dfWrong,class))
[1] TRUE
谢谢
推荐答案
您可以尝试以下方法:
dfWrong[] <- mapply(FUN = as,dfWrong,sapply(dfCorrect,class),SIMPLIFY = FALSE)
...虽然我的本能是同意奥利弗(Oliver)的看法,但如果是我,我会尽力确保在读取数据时使用正确的课程.
...although my first instinct is to agree with Oliver that if it were me I'd try to ensure the correct class at the point you're reading the data.
这篇关于R将列类从一个数据帧分配(或复制)到另一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!