本文介绍了R 将列类从一个数据框中分配(或复制)到另一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我生成了一个大型数据框(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 将列类从一个数据框中分配(或复制)到另一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 17:17