r - R: if values of two columns of two data frames are equal, add column of one of the two data frames
问题描述
I have two data frames
columns df1= a, b, c, d
columns df2= e, f, c, d
I want to say that if df1$a == df2$e
and at the same time df1$b == df2$f
I want to print in a new column df1$x
the values of the corresponding df2$c
.
So I wrote this command df1$x <- ifelse(df1$a %in% df2$e & df1$b %in% df2$f, df2$c, ifelse (NULL))
But the values in df1x
are not correct. I think I have to do a while loop, but I have no idea from where to start.
Nicoletta
解决方案
To merge two data frames by multiple columns you can use merge
and corresponding by.x
and by.y
. Both of those accept vector of column names. Parameter all.x
means all data from first df1
will be preserved.
df1 <- merge(df1, df2[,c("e","f","c")], by.x=c("a","b"), by.y=c("e","f"), all.x=T)
colnames(df1) <- c("a","b","c","d","x")
For more information see ?merge
. For more complex operation you can check out package dplyr
and its function left_join
.
Result on random data
df1 <- data.frame(a = sample(1:5), b= sample(1:5), c=runif(5), d = runif(5))
df2 <- data.frame(e = df1$a, f = df1$b, c = runif(5), d = runif(5))
df2$e[3] <- 7
Output
> df1
a b c d
1 5 4 0.76677063 0.92123552
2 4 1 0.93524320 0.09275425
3 3 2 0.01121468 0.12035981
4 1 5 0.72992427 0.87711572
5 2 3 0.11680937 0.93696597
> df2$e[3] <- 7
> df2
e f c d
1 5 4 0.6251662 0.1549575
2 4 1 0.8464672 0.8702837
3 7 2 0.5394273 0.4290171
4 1 5 0.4061817 0.9072905
5 2 3 0.3376456 0.4291463
> merge(df1, df2[,c("e","f","c")], by.x=c("a","b"), by.y=c("e","f"), all.x=T)
a b c d x
1 1 5 0.72992427 0.87711572 0.4061817
2 2 3 0.11680937 0.93696597 0.3376456
3 3 2 0.01121468 0.12035981 NA
4 4 1 0.93524320 0.09275425 0.8464672
5 5 4 0.76677063 0.92123552 0.6251662
推荐阅读
- akka - Akka 中的无类型 ActorSystem 是什么?
- robotframework - ${time} = 获取时间 time=NOW + 1h 2min 3s # 1h 2min 3s 添加到本地时间
- r - 在数据框R中创建日期序列作为新列
- jenkins - 何时在詹金斯 CI 中使用 jmeter 进行性能测试?
- javascript - 如何为 Bokeh 回调过滤器的一部分编写 Javascript 代码,以便我可以按数字和类别进行过滤?
- primes - 使用 Raku 中的惰性列表获取前 10 位素数
- android - 我是编码 Android 工作室的新手。我试图实现 PlacePicker
- tfs - 从 Windows 10 客户端上的大文件下载 TFS 2018 Web 访问不再可能
- python - 每列的数据类型或 dtype 是否正确设置?好吧,我理解这个问题,但我几乎没有疑问
- python - 如何使用 cPanel 部署 Django 网站?