简体   繁体   English

如何使用 base 或 dplyr 通过不同列中的相同值合并 R 中的两个数据框?

[英]How can i merge two data frames in R by the same values in different columns using base or dplyr?

Let's say that I have two data frames In R :假设我在 R 中有两个数据框:

The fIrst one:第一个:

cat = rep("xx",5);cat
fIrst = c("a","a","a","h","h")
second = c("b","c","d","b","c")
val1 = c(1,2,3,10,20)
A = tIbble(cat,fIrst,second,val1);A

that looks lIke thIs :看起来像这样:

# A tIbble: 5 x 4
  cat   fIrst second  val1
  <chr> <chr> <chr>  <dbl>
1 xx    a     b          1
2 xx    a     c          2
3 xx    a     d          3
4 xx    h     b         10
5 xx    h     c         20

and a second one :第二个:

cat = rep("xx",5);cat
fIrst = c("a","a","a","b","c")
second = c("b","c","d","h","h")
val2 = c(100,200,300,400,500)
B = tIbble(cat,fIrst,second,val2);B

that Is :那是 :

# A tIbble: 5 x 4
  cat   fIrst second  val2
  <chr> <chr> <chr>  <dbl>
1 xx    a     b        100
2 xx    a     c        200
3 xx    a     d        300
4 xx    b     h        400
5 xx    c     h        500

I want to merge them but when I do It I get :我想合并它们,但是当我这样做时,我得到:

left_joIn(A,B,by=c("cat","fIrst","second"))
# A tIbble: 5 x 5
  cat   fIrst second  val1  val2
  <chr> <chr> <chr>  <dbl> <dbl>
1 xx    a     b          1   100
2 xx    a     c          2   200
3 xx    a     d          3   300
4 xx    h     b         10    NA
5 xx    h     c         20    NA

because h Is In dIfferent column when I try to joIn them.因为当我尝试加入它们时,h 在不同的列中。 Because the combInatIons are the same but In dIfferent order.因为组合相同但顺序不同。

Ideally I want to look lIke thIs :理想情况下,我想看起来像这样:

cat fIrst第一的 second第二 val1 val1 val2 val2
xx xx a一个 b b 1 1 100 100
xx xx a一个 c C 2 2 200 200
xx xx a一个 d d 3 3 300 300
xx xx h H b b 10 10 400 400
xx xx h H c C 20 20 500 500

how can I do that ?我怎样才能做到这一点 ? Any help?有什么帮助吗?

You can do this simply by binding the val2 column to the first dataframe (tibble) using base R's cbind function.您可以通过使用基本 R 的cbind函数将val2列绑定到第一个数据帧(tibble)来简单地做到这一点。
cbind(A, B$val2) would do the job. cbind(A, B$val2)可以完成这项工作。 You are having problems because h, b & h, c pairs are absent in the tibble B but you are expecting R to superimpose the values of B$val2[c(4,5)] in place where the first & second keys conflict in the tibbles A & B. You cannot expect R to magically bind data where there are conflicting keys.您遇到问题是因为 tibble B中不存在h, b & h, c对,但您希望 R 将B$val2[c(4,5)]的值叠加在firstsecond键冲突的地方小标题 A 和 B。你不能指望 R 神奇地绑定存在冲突键的数据。 If you want to keep all keys you can use the dplyr::full_join() but it'll introduce NA s where there are conflicting keys.如果要保留所有密钥,可以使用dplyr::full_join()但它会在密钥冲突的地方引入NA (By keys I mean the values you pass for the by argument!) (按键是指您为by参数传递的值!)

And it's a frowned upon practice to use equal sign = for assignments in R. Try using -> instead.在 R 中使用等号=进行赋值是一种不习惯的做法。尝试使用->代替。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Dplyr 检查 R 中的变量分组的 2 个数据帧的两列的值是否存在差异? - How can I check the values of two columns of 2 data frames for discrepancies grouped by a variable in R using Dplyr? 如何合并R中具有相同列的多个数据框? - How can I merge multiple data frames with the same columns in R? 如何使用dplyr包将R中公共列上的两个数据帧与其他总和合并 - How to merge two data frames on common columns in R with sum of others using dplyr package plyr如何合并两列具有相同名称但不同值的不同data.frames - How does plyr merge two columns of different data.frames with same names but different values 如何在R中使用merge()合并列上的数据帧? - How can I use merge() in R to merge data frames on columns? 我如何使用 dplyr 计算 R 中两个数据帧之间的相关性? - How i can calculate correlation between two data frames in R using dplyr? 如何使用具有多个条件的 dplyr 交叉检查 R 中的两个数据帧? - How can I cross check two data frames in R using dplyr with multiple conditions? 如何在R中使用inner_join,dplyr将两个数据帧连接成两列,这些数据帧应该同时匹配? - How to join two data frames by 2 columns which should be matched at the same time with using inner_join, dplyr in R? R:如何合并更多 2 个数据框并添加值? - R: How can i merge more 2 data frames with adding values? R - 将两个不同长度的数据帧比较为两列中的相同值 - R - Compare two data frames of different length for same values in two columns
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM