在 r 中查找并提取具有匹配属性的数据帧行
Find and extract dataframe rows with matching attributes in r
我在 r 中有一个数据框,其中每一行记录了给定时间在给定地点的某些动物物种的丰度。我想提取所有具有某种匹配标准的成对记录集。此处的示例数据:http://tinyurl.com/qeqxx25
对于 WDPAID、Sp 和方法具有相同值的每一对行(例如,Record.Number 8 和 9),我想 return 具有值的单个记录对于每对记录的每一列。例如,对于 Record.Numbers 8 & 9,我想:http://tinyurl.com/nl3jn5b
任何朝着正确方向的推动都会受到赞赏。
我会通过将 table 合并到自身来做到这一点。这些行中的东西
## Getting and preparing data
>>df=data.frame(Record = c(8,9), WDPAID = c(1, 1), Year= c(2005, 2006), Method=c("a","a"))
>>df$ind= 1:nrow(df)
>>df2= df
>> df
Record WDPAID Year Method ind
1 8 1 2005 a 1
2 9 1 2006 a 2
## doing the merge
>> df_noisy = merge(df, df2, by.x = c("Method", "WDPAID") , by.y = c("Method","WDPAID"), suffixes=c(1,2))
>> df_needed = df_noisy[df_noisy$ind1 != df_noisy$ind2, ]
初始数据集
>> df_needed
Method WDPAID Record1 Year1 ind1 Record2 Year2 ind2
2 a 1 8 2005 1 9 2006 2
3 a 1 9 2006 2 8 2005 1
我在 r 中有一个数据框,其中每一行记录了给定时间在给定地点的某些动物物种的丰度。我想提取所有具有某种匹配标准的成对记录集。此处的示例数据:http://tinyurl.com/qeqxx25
对于 WDPAID、Sp 和方法具有相同值的每一对行(例如,Record.Number 8 和 9),我想 return 具有值的单个记录对于每对记录的每一列。例如,对于 Record.Numbers 8 & 9,我想:http://tinyurl.com/nl3jn5b
任何朝着正确方向的推动都会受到赞赏。
我会通过将 table 合并到自身来做到这一点。这些行中的东西
## Getting and preparing data
>>df=data.frame(Record = c(8,9), WDPAID = c(1, 1), Year= c(2005, 2006), Method=c("a","a"))
>>df$ind= 1:nrow(df)
>>df2= df
>> df
Record WDPAID Year Method ind
1 8 1 2005 a 1
2 9 1 2006 a 2
## doing the merge
>> df_noisy = merge(df, df2, by.x = c("Method", "WDPAID") , by.y = c("Method","WDPAID"), suffixes=c(1,2))
>> df_needed = df_noisy[df_noisy$ind1 != df_noisy$ind2, ]
初始数据集
>> df_needed
Method WDPAID Record1 Year1 ind1 Record2 Year2 ind2
2 a 1 8 2005 1 9 2006 2
3 a 1 9 2006 2 8 2005 1