2016-12-11 140 views
3

我的数据是低于我想基于ID时,我只保留那些具有相似的ID,以将其分成几个部分拆分数据分成几个部分

df1<- structure(list(Ids1 = 1:7, string1 = structure(c(3L, 2L, 4L, 
1L, 1L, 1L, 1L), .Label = c("gdyijq,udyhfs,gqdtr", "hdydg", "hishsgd,gugddf", 
"ydis"), class = "factor"), Ids2 = c(1L, 3L, 4L, 9L, 10L, NA, 
NA), string2 = structure(c(4L, 6L, 2L, 3L, 5L, 1L, 1L), .Label = c("", 
"gdyijq,udyhfs", "gqdtr", "hishsgd,gugddf", "nlrshf", "ydis"), class = "factor")), .Names = c("Ids1", 
"string1", "Ids2", "string2"), class = "data.frame", row.names = c(NA, 
-7L)) 

首先我要让df.1并计算string1与string2的相似程度(用逗号分隔)。

Ids1 string1   ids2 string2   Similar 
1 hishsgd,gugddf  1  hishsgd,gugddf  2 
3 ydis     3  ydis    1 
4 gdyijq,udyhfs,gqdtr 4  gdyijq,udyhfs  2 

我这样做

df.1 <- df1[which(df1$Ids1 == df1$Ids2), ] 

只给我的第一行,并没有别的

然后我想有那些有唯一ID号为1,其不要在IDS2

存在
Ids1 string1 
2  hdydg 
5  gdyijq,udyhfs,gqdtr 
6  gdyijq,udyhfs,gqdtr 
7  gdyijq,udyhfs,gqdtr 

我这样做,但也不起作用

df.2<- df1[which(df1$Ids1 != df1$Ids2), ] 

,最后我想保留那些只在IDS2而不是IDS1

Ids1 string1 
9  gqdtr 
10  nlrshf 

,我这样做,但也不起作用

df.3<- df1[which(df1$Ids2 != df1$Ids1), ] 

回答

1

这里是一个解决方案,我能拿出基于使用dplyr的加入包:

library(dplyr) 

df.1 <- inner_join(select(df1, Ids1, string1), select(df1, Ids2, string2), by = c('Ids1' = 'Ids2')) 
df.1$Similar <- apply(df.1[, -1], 1, function(x) sum(unlist(strsplit(x[1], ',')) %in% unlist(strsplit(x[2], ',')))) 

df.2 <- anti_join(select(df1, Ids1, string1), select(df1, Ids2, string2), by = c('Ids1' = 'Ids2')) 
df.3 <- anti_join(select(df1, Ids2, string2), select(df1, Ids1, string1), by = c('Ids2' = 'Ids1')) 
df.3 <- df.3[complete.cases(df.3), ] 

你也可以做一些不同的df.2和df.3如下:

df.2 <- df1[!df1$Ids1 %in% df1$Ids2, c('Ids1', 'string1')] 
df.3 <- df1[!df1$Ids2 %in% df1$Ids1, c('Ids2', 'string2')] 
df.3 <- df.3[complete.cases(df.3), ] 
+0

谢谢我接受并喜欢你的答案 – nik