首页 > 解决方案 > 绑定两个数据帧并删除 R 中的相同行号

问题描述

我有两个行数相同(639)但列长不同(DF1:5,DF2:2500)的数据帧,DF1 中的行对应于 DF2 中的行。

DF2 中的某些行将由于几个而被删除NAs,但我没有关于哪些行被删除的信息。cbind()由于现在不同的行长,不允许我将 DF 绑定在一起。但是,我还需要对应的行,因此如果在 DF2 中删除了第 47 行,则在合并时也必须在 DF1 中删除它。我的假设是可以有一些解决方法,row.names但我不确定如何执行它。帮助将不胜感激。以下 DF 示例:

DF1:

    pp trialNo  item trialTarget trial
1 pp01       1  M012      script     1
2 pp01       2 BS016      script     2
3 pp01       3  M007      script     3
4 pp01       4 BS010      script     4
5 pp01       5  M006      script     5
6 pp01       6 BS018      script     6 

DF2:

    V1  V2  V3  V4  V5  V6
1: 764 764 763 763 762 763
2: 714 714 711 708 705 704
3: 872 871 869 867 867 871
4: 730 728 727 724 722 719
5: 789 786 788 790 792 790
6: 922 923 928 933 938 938

并假设删除了 DF2 中的第 3 行,我希望在绑定后会这样:

    pp trialNo  item trialTarget trial  V1  V2  V3  V4  V5  V6
1 pp01       1  M012      script     1 764 764 763 763 762 763
2 pp01       2 BS016      script     2 714 714 711 708 705 704
4 pp01       4 BS010      script     4 730 728 727 724 722 719
5 pp01       5  M006      script     5 789 786 788 790 792 790
6 pp01       6 BS018      script     6 922 923 928 933 938 938

提前致谢。

标签: rdataframemergecbind

解决方案


您可以在每个数据框中创建一个行索引。

df1$row <- 1:nrow(df1)
df2$row <- 1:nrow(df2)

然后删除中的第 3 行df2

df2 <- df2[-3, ]

然后,您可以mergerow列两个数据框。

merge(df1, df2, by = 'row')

#  row   pp trialNo  item trialTarget trial  V1  V2  V3  V4  V5  V6
#1   1 pp01       1  M012      script     1 764 764 763 763 762 763
#2   2 pp01       2 BS016      script     2 714 714 711 708 705 704
#3   4 pp01       4 BS010      script     4 730 728 727 724 722 719
#4   5 pp01       5  M006      script     5 789 786 788 790 792 790
#5   6 pp01       6 BS018      script     6 922 923 928 933 938 938

数据

df1 <- structure(list(pp = c("pp01", "pp01", "pp01", "pp01", "pp01", 
"pp01"), trialNo = 1:6, item = c("M012", "BS016", "M007", "BS010", 
"M006", "BS018"), trialTarget = c("script", "script", "script", 
"script", "script", "script"), trial = 1:6, row = 1:6), row.names = c(NA, 
-6L), class = "data.frame")

df2 <- structure(list(V1 = c(764L, 714L, 872L, 730L, 789L, 922L), V2 = c(764L, 
714L, 871L, 728L, 786L, 923L), V3 = c(763L, 711L, 869L, 727L, 
788L, 928L), V4 = c(763L, 708L, 867L, 724L, 790L, 933L), V5 = c(762L, 
705L, 867L, 722L, 792L, 938L), V6 = c(763L, 704L, 871L, 719L, 
790L, 938L)), class = "data.frame", row.names = c(NA, -6L))

推荐阅读