R在列中查找相同的值以获得另一列的唯一值

R在列中查找相同的值以获得另一列的唯一值,r,unique,R,Unique,下面是一些示例数据 sample = data.frame("col1" = c("val1", "val1", "val1", "val1", "val2", "val2", "val2", "val3", "val3", "val3", "val3"), "col2" = c("this", "that", "some", "thing", "thing", "that", "some", "diff", "some", "this", "that")

下面是一些示例数据

sample = data.frame("col1" = c("val1", "val1", "val1", "val1", "val2", "val2", "val2", "val3", "val3", "val3", "val3"),
                    "col2" = c("this", "that", "some", "thing", "thing", "that", "some", "diff", "some", "this", "that"))
我想确定col2列的每个条目,它出现在col1列的每个唯一值中。这有可能吗? 这将是样本数据的结果:

result = c("that", "some")

提前感谢。

另一个肮脏的基础
R
解决方案:

names(which(table(unlist(aggregate(sample$col2, list(sample$col1), unique)[, 2])) == length(unique(sample$col1))))

[1] "some" "that"

另一个脏基
R
解决方案:

names(which(table(unlist(aggregate(sample$col2, list(sample$col1), unique)[, 2])) == length(unique(sample$col1))))

[1] "some" "that"
base R
中的(快速且肮脏)解决方案:

sample_list <- split(sample, sample$col1)
for (i in 1:length(sample_list)) sample_list[[i]] <- sample_list[[i]]$col2
Reduce(intersect, sample_list)
[1] "that" "some"
这个解决方案在大数据集上速度很快

编辑2:

dcast
,可在
数据中找到。表

present_in <- colSums(!is.na(dcast(sample, col1 ~ col2, value.var = "col2")))
names(present_in)[present_in == 3][-1]
[1] "some" "that"
在中呈现\u
基本R中的(快速且肮脏)溶液

sample_list <- split(sample, sample$col1)
for (i in 1:length(sample_list)) sample_list[[i]] <- sample_list[[i]]$col2
Reduce(intersect, sample_list)
[1] "that" "some"
这个解决方案在大数据集上速度很快

编辑2:

dcast
,可在
数据中找到。表

present_in <- colSums(!is.na(dcast(sample, col1 ~ col2, value.var = "col2")))
names(present_in)[present_in == 3][-1]
[1] "some" "that"

在中使用
dplyr
呈现\u

require(dplyr)

sets <- length(unique(sample$col1))

s <- sample %>%
    group_by(col2) %>%
    summarise(n = n_distinct()) %>%
    filter(n == sets)

result <- s$col2
[1] some that
require(dplyr)
设置%
总结(n=n_distinct())%>%
过滤器(n==组)

结果这里有一个使用
dplyr
的循环方式

require(dplyr)

sets <- length(unique(sample$col1))

s <- sample %>%
    group_by(col2) %>%
    summarise(n = n_distinct()) %>%
    filter(n == sets)

result <- s$col2
[1] some that
require(dplyr)
设置%
总结(n=n_distinct())%>%
过滤器(n==组)

结果这是使用dplyr的一种方法:

split(sample,sample$col1)%>%
Reduce(function(dtf1,dtf2) inner_join(dtf1,dtf2,by="col2"), .)%>%select(col2)%>%print()

  col2
1 that
2 some

这是使用dplyr执行此操作的一种方法:

split(sample,sample$col1)%>%
Reduce(function(dtf1,dtf2) inner_join(dtf1,dtf2,by="col2"), .)%>%select(col2)%>%print()

  col2
1 that
2 some

您需要的是
intersect
。这里有一个快速而肮脏的方法:

代码

library(data.table)
dt <- as.data.table(sample) 

# Split data.table into different chunks based on unique values in col1
# output is a list where each entry is a data.table 
l <- split(dt, by = "col1")

# Find the intersection of all values in col2 
Reduce(intersect, lapply(1:length(l), function(z) as.character(l[[z]]$col2)))

您需要的是
intersect
。这里有一个快速而肮脏的方法:

代码

library(data.table)
dt <- as.data.table(sample) 

# Split data.table into different chunks based on unique values in col1
# output is a list where each entry is a data.table 
l <- split(dt, by = "col1")

# Find the intersection of all values in col2 
Reduce(intersect, lapply(1:length(l), function(z) as.character(l[[z]]$col2)))

对于一个
col1
值,单个
col2
值能否多次出现?像
“val1”
中的
“this”
两次一样,一个
col2
值能否为一个
col1
值多次出现?像
“val1”
中的两次“this”
?这不一定正确。对于相同的
col1
值,可以有多个
col2
条目。可以通过在
s
dplyr
has
n_distinct()
的第二行中添加
distinct
来修复,以替换
length(unique())
正确同意@bouncyball解决方案不正确。呼叫正确。这不一定正确。对于相同的
col1
值,可以有多个
col2
条目。可以通过在
s
dplyr
has
n_distinct()
的第二行中添加
distinct
来修复,以替换
length(unique())
正确同意@bouncyball解决方案不正确。呼叫正确。将修复