【问题标题】:subseting a data frame under a specific condition在特定条件下对数据框进行子集化
【发布时间】:2016-08-08 05:46:16
【问题描述】:

如何获得与另一个数据框比较的元素中具有相同值的数据框行? 我已经写了这个,但是没有用。

 # example of two data frame

    df1 <- data.frame(V1 = c("a", "g", "h", "l", "n", "e"), V2 = c("b", "n", "i", "m", "i", "f"), stringsAsFactors = F)

    df2 <- data.frame(V1 = c("a", "c", "f","h"), V2 = c("b", "d", "e","z"), stringsAsFactors = F)

    # finding joint values in each element of two data frames
    res1<-intersect(df1$V1,df2$V1)
    res2<-intersect(df1$V2,df2$V2)
    res3<-intersect(df1$V1,df2$V2)
    res4<-intersect(df1$V1,df2$V2)
    # Getting rows that has joint value at least in one element of df1
    ress1<-df1[apply(df1, MARGIN = 1, function(x) all(x== res1)), ]
    ress2<-df1[apply(df1, MARGIN = 1, function(x) all(x== res2)), ]
    ress3<-df1[apply(df1, MARGIN = 1, function(x) all(x== res3)), ]
    ress4<-df1[apply(df1, MARGIN = 1, function(x) all(x== res4)), ]

    # Getting rows that has joint value at least in one element of df2
    resss1<-df2[apply(df2, MARGIN = 1, function(x) all(x== res1)), ]
    resss2<-df2[apply(df2, MARGIN = 1, function(x) all(x== res2)), ]
    resss3<-df2[apply(df2, MARGIN = 1, function(x) all(x== res3)), ]
    resss4<-df2[apply(df2, MARGIN = 1, function(x) all(x== res4)), ]

    # then combine above results

    final.res<-rbind(ress1,ress2,ress3,ress4,resss1,resss2,resss3,resss4)

我最喜欢的结果是:

a   b
h   z
h   i
f   e
e   f

【问题讨论】:

  • 你想要的输出中的 Z 来自哪里?
  • 对不起,这是一个错误,我已修复它

标签: r subset apply substitution intersect


【解决方案1】:

这应该可以工作

#Import data
df1 <- data.frame(V1 = c("a", "g", "h", "l", "n", "e"), V2 = c("b", "n", "i", "m", "i", "f"), stringsAsFactors = F)  
df2 <- data.frame(V1 = c("a", "c", "f","h"), V2 = c("b", "d", "e","z"), stringsAsFactors = F)

# Get the intersects
vals <- intersect(c(df1$V1, df1$V2), c(df2$V1, df2$V2))

#Get the subsets and rbind them
full <- rbind(
 subset(df1, df1$V1 %in% vals),
 subset(df1, df1$V2 %in% vals),
 subset(df2, df2$V1 %in% vals),
 subset(df2, df2$V2 %in% vals)
)

#Remove duplicates  
full <- full[!duplicated(full),]

【讨论】:

    猜你喜欢
    • 2021-06-05
    • 1970-01-01
    • 2022-06-18
    • 2015-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多