rbind.data.frames根据一列R保留唯一行 [英] rbind.data.frames keeping unique rows according to one column R

查看:84
本文介绍了rbind.data.frames根据一列R保留唯一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个不同的数据框,分别显示2014年和2015年的问卷调查结果,且均在同一列中。我想将2014年的唯一行(即ID码)添加到2015年的数据框中。

I have two different data frame showing questionnaire-results from 2014 and 2015, both with the same columns. I want to add the unique rows (i,e. ID-codes) from 2014 to the data frame from 2015.

问题是所有行都是唯一的(因为有23列),但我只想添加包含唯一ID码的行(其中一列),即回答问卷调查表2014而不是2015的人。

The problem is that all rows are unique (since there is 23 columns), but I only wish to add the rows containing an unique ID-code (one of the columns), i.e. people that answered the questionnaire 2014 but not 2015.

使用rbind.data.frames成功创建了 2014-2015数据框,但随后我想删除包含ID码的行,这些行既响应2014年,也响应2015年,并保留了2015年的响应。

Using rbind.data.frames succeedes in creating an "2014-2015 dataframe" but then I want to erase the rows containg ID-codes that answered both 2014 and 2015 and keeo those from 2015.

推荐答案

如果我对您的解释正确:

If I interpret you correctly:

df2014<-structure(list(ID = 1:7, V1 = structure(c(1L, 1L, 1L, 2L, 2L, 
3L, 1L), .Label = c("A", "B", "C"), class = "factor"), V2 = structure(c(2L, 
1L, 2L, 1L, 2L, 3L, 1L), .Label = c("A", "B", "C"), class = "factor"), 
V3 = structure(c(1L, 1L, 2L, 3L, 3L, 3L, 1L), .Label = c("A", 
"B", "C"), class = "factor")), .Names = c("ID", "V1", "V2", 
"V3"), class = "data.frame", row.names = c(NA, -7L))

df2015<-structure(list(ID = 4:10, V1 = structure(c(1L, 1L, 1L, 2L, 2L, 
3L, 1L), .Label = c("A", "B", "C"), class = "factor"), V2 = structure(c(2L, 
1L, 2L, 1L, 2L, 3L, 1L), .Label = c("A", "B", "C"), class = "factor"), 
V3 = structure(c(1L, 1L, 2L, 3L, 3L, 3L, 1L), .Label = c("A", 
"B", "C"), class = "factor")), .Names = c("ID", "V1", "V2", 
"V3"), class = "data.frame", row.names = c(NA, -7L))

library(dplyr)
rbind(dplyr::filter(df2014, !ID %in% intersect(df2014$ID, df2015$ID)), df2015)

结果数据框

    ID V1 V2 V3
1   1  A  B  A
2   2  A  A  A
3   3  A  B  B
4   4  A  B  A
5   5  A  A  A
6   6  A  B  B
7   7  B  A  C
8   8  B  B  C
9   9  C  C  C
10 10  A  A  A

这篇关于rbind.data.frames根据一列R保留唯一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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