将多个列从一个data.frame复制到另一个 [英] Copying multiple columns from one data.frame to another

查看:647
本文介绍了将多个列从一个data.frame复制到另一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个简单的方法来使R自动将数据列从data.frame复制到另一个?

 > DF1<  -  data.frame(a = 1:3,b = 4:6)
> DF2< - 数据。框架(c = -2:0,d = 3:1)

我想得到一些东西像

 > DF1 
abcd
1 -2 4 -2 3
2 -1 5 -1 2
3 0 6 0 1

我通常会手动,如

  DF1 $ c < -  DF2 $ c 
DF1 $ d < - DF2 $ d $只要我有几个变量就可以了,但是变得非常耗时,容易发生错误的时候,b $ b

处理几个变量。关于如何有效地做到这一点的想法?这可能很简单,但是我发誓我无法找到答案谷歌,谢谢!

解决方案

示例不正确,应该是:

 > DF1 $ c < -  DF2 $ c 
> DF1 $ d< - DF2 $ d
> DF1
abcd
1 1 4 -2 3
2 2 5 -1 2
3 3 6 0 1

然后 cbind 完全一样:

 > cbind(DF1,DF2)
abcd
1 1 4 -2 3
2 2 5 -1 2
3 3 6 0 1


Is there a simple way to make R automatically copy columns from a data.frame to another?

I have something like:

>DF1 <- data.frame(a=1:3, b=4:6)
>DF2 <- data.frame(c=-2:0, d=3:1)

and I want to get something like

>DF1
   a b  c d
1 -2 4 -2 3
2 -1 5 -1 2
3  0 6  0 1

I'd normally do it by hand, as in

DF1$c <- DF2$c
DF1$d <- DF2$d

and that's fine as long as I have few variables, but it becomes very time consuming and prone to error when dealing with several variables. Any idea on how to do this efficiently? It's probably quite simple but I swear I wasn't able to find an answer googling, thank you!

解决方案

The result from your example is not correct, it should be:

> DF1$c <- DF2$c
> DF1$d <- DF2$d
> DF1
  a b  c d
1 1 4 -2 3
2 2 5 -1 2
3 3 6  0 1

Then cbind does exactly the same:

> cbind(DF1, DF2)
  a b  c d
1 1 4 -2 3
2 2 5 -1 2
3 3 6  0 1

这篇关于将多个列从一个data.frame复制到另一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆