Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 基于同一ID列从两个数据帧中提取值_R - Fatal编程技术网

R 基于同一ID列从两个数据帧中提取值

R 基于同一ID列从两个数据帧中提取值,r,R,我想从两个不同数据帧中的值创建一个数据帧 在列ID中包含相同的值 输入数据 df1我们可以使用left\u连接和过滤器 library(dplyr) left_join(df1, df2, by = 'ID') %>% filter(Order1 == 'ball') %>% select(names(df1), starts_with('ActOrder')) 可能重复我希望基于df1的列Order1(ball)中的值将值提取到新的数据帧中。列“Order1”、“A

我想从两个不同数据帧中的值创建一个数据帧 在列ID中包含相同的值

输入数据
df1我们可以使用
left\u连接
过滤器

library(dplyr)
left_join(df1, df2, by = 'ID') %>%
   filter(Order1 == 'ball')  %>%
   select(names(df1), starts_with('ActOrder'))

可能重复我希望基于df1的列Order1(ball)中的值将值提取到新的数据帧中。列“Order1”、“ActOrder1”和“ActOrder2”中的其他值不匹配。但是“ID”列中相应的值是匹配的。因为您在下面的评论中询问了关于左连接和右连接的更多信息,所以我标记为的文章有19个答案,提供了关于不同类型连接的大量详细信息。单击链接^^^^不会生成所需的输出。@AashayMehta如果“df3”是预期的输出,则更新会给出预期的输出。如果df2有多个列,并且我只想提取ActOrder1和ActOrder2中的值,则我需要做什么更改?@AashayMehta您仍然需要“ID”和“Order1”列吗,在df3中仍然需要ID和Order1列
df3 <- data.frame(ID = c(102, 205), Order1 = c("ball", "ball"), 
ActOrder1 = c("cap", "watch"), ActOrder2 = c("cap", "fan") )
library(dplyr)
left_join(df1, df2, by = 'ID') %>%
   filter(Order1 == 'ball')  %>%
   select(names(df1), starts_with('ActOrder'))