Dataframe 查找数据帧列表之间的唯一变量相交
我面临着一个问题,试图找到列表中所有s之间的共同变量 我找到了。 但是它没有回答我的问题,因为他们只使用Dataframe 查找数据帧列表之间的唯一变量相交,dataframe,r,list,Dataframe,R,List,我面临着一个问题,试图找到列表中所有s之间的共同变量 我找到了。 但是它没有回答我的问题,因为他们只使用colname来进行比较,而我对列中的变量感兴趣 首先,我有一个数据帧列表,我们称之为list1 >list1 [[1]] V1 V2 V3 1 "a" 1 2 "b" 9 3 "c" 3 [[2]] V1 V2 V3 1 "c" 5 2 "d" 4 3 "e" 6 #and so on..... for 22 times
colname
来进行比较,而我对列中的变量感兴趣
首先,我有一个数据帧列表,我们称之为list1
>list1
[[1]]
V1 V2 V3
1 "a" 1
2 "b" 9
3 "c" 3
[[2]]
V1 V2 V3
1 "c" 5
2 "d" 4
3 "e" 6
#and so on..... for 22 times
现在,我想输出所有list1[[I]]$V2
变量的数组,这些变量在所有dataframe
s之间是公共的。因此,如果剩余的20个dataframe
s看起来都像list1[[2]]
,那么输出应该是c
;因为它将是所有数据帧
之间唯一通用的V2
变量
我曾尝试使用do.call(“rbind”,list1)
和dplyr
来查找常见的V2
s,但我似乎无法找到它。另外,我知道在这个例子中可以使用intersect()
,但是使用intersect(intersect(intersect…
)似乎是解决这个问题的一个非常低效的方法,我想在其他列表上也做这个操作。任何帮助都将不胜感激
多谢各位
-Omar.这是一个
tidyverse
解决方案,使用purr::map
和purr::reduce
library(tidyverse)
set.seed(999)
#first generate some data
dflist <- map(1:3,~tibble(V1=sample(letters[1:5],3),V2=sample(1:5,3)))
dflist
[[1]]
# A tibble: 3 x 2
V1 V2
<chr> <int>
1 b 5
2 c 4
3 a 1
[[2]]
# A tibble: 3 x 2
V1 V2
<chr> <int>
1 d 4
2 a 2
3 b 5
[[3]]
# A tibble: 3 x 2
V1 V2
<chr> <int>
1 a 4
2 d 1
3 e 3
#then...
map(dflist, ~.$V1) %>% #create a list just of the column of interest
reduce(intersect) #apply the intersect function cumulatively to the list
[1] "a"
库(tidyverse)
种子集(999)
#首先生成一些数据
dflist%#创建一个感兴趣列的列表
减少(相交)#将相交函数累积应用于列表
[1] “a”
这里有一个tidyverse
解决方案,使用purr::map
和purrr::reduce
library(tidyverse)
set.seed(999)
#first generate some data
dflist <- map(1:3,~tibble(V1=sample(letters[1:5],3),V2=sample(1:5,3)))
dflist
[[1]]
# A tibble: 3 x 2
V1 V2
<chr> <int>
1 b 5
2 c 4
3 a 1
[[2]]
# A tibble: 3 x 2
V1 V2
<chr> <int>
1 d 4
2 a 2
3 b 5
[[3]]
# A tibble: 3 x 2
V1 V2
<chr> <int>
1 a 4
2 d 1
3 e 3
#then...
map(dflist, ~.$V1) %>% #create a list just of the column of interest
reduce(intersect) #apply the intersect function cumulatively to the list
[1] "a"
库(tidyverse)
种子集(999)
#首先生成一些数据
dflist%#创建一个感兴趣列的列表
减少(相交)#将相交函数累积应用于列表
[1] “a”
您需要发布创建有效示例的代码,并说明什么是正确答案。阅读和回答更具体的R-SO问题:“如何在R中创建一个伟大的可复制示例”。不幸的是,您的问题描述似乎不清楚。您需要发布创建有效示例的代码,并说明什么是正确答案。请阅读和回答更具体的问题:“如何在R中创建一个伟大的可复制示例”。不幸的是,您的问题描述似乎不清楚。