简体   繁体   English

r - Select DF 基于另一个 DF 列中的值

[英]r - Select DF based on value in column of another DF

I want to bind the columns of 2 dataframes.我想绑定 2 个数据框的列。 The second dataframe should be choosen among several dataframes in my environment, depending on the value of a cell in the first dataframe.第二个 dataframe 应该在我的环境中的几个数据帧中选择,具体取决于第一个 dataframe 中的单元格的值。

Sample Data样本数据

test <- structure(list(main_activity = structure(c(1L, 9L), .Label = c("Manufacturing", 
"Manufacturing; Retail", "Manufacturing; Services", "Manufacturing; Wholesale", 
"Manufacturing; Wholesale; Services", "Retail", "Retail; Services", 
"Retail; Wholesale", "Services", "Services; Manufacturing", "Services; Retail", 
"Services; Wholesale", "Wholesale", "Wholesale; Manufacturing", 
"Wholesale; Retail", "Wholesale; Services"), class = "factor"), 
    p_l_for_period_net_income_th_eur_2019 = c(-4849.968, -4416.404
    ), Name = c("A", "B")), class = "data.frame", row.names = c(NA, 
-2L))

Manufacturing_2015 <- as.data.frame(matrix(data = c(2000)))
Services_2015 <- as.data.frame(matrix(data = c(3000)))

What I want to be able to do is to check the value of the column 'main_activity' and bind the dataframe that matches that name with the 'test' dataframe.我想要做的是检查列'main_activity'的值并将与该名称匹配的dataframe与'test'dataframe绑定。 So, the goal is to have the following:因此,目标是具有以下内容:

Desired Outcome期望的结果

binded_A <- cbind(test %>% filter(Name == "A"), Manufacturing_2015)
binded_B <- cbind(test %>% filter(Name == "B"), Services_2015)

Is there a way to do it automatically?有没有办法自动完成?

You can use ls to select a data frame from the global environment based on it's value in main_activity and cbind the data to original subset of dataframe.您可以使用ls到 select 一个来自全局环境的数据帧,基于它在main_activity中的值,并将数据绑定到cbind的原始子集。

result <- lapply(test$main_activity, function(x) cbind(subset(test, 
               main_activity == x), get(ls(pattern = x, envir = .GlobalEnv))))

result

#[[1]]
#  main_activity p_l_for_period_net_income_th_eur_2019 Name   V1
#1 Manufacturing                             -4849.968    A 2000

#[[2]]
#  main_activity p_l_for_period_net_income_th_eur_2019 Name   V1
#2      Services                             -4416.404    B 3000

This will return you a list of dataframes, if you need them as separate dataframes name them and use list2env .如果您需要将它们作为单独的数据框命名并使用list2env ,这将返回您的数据框列表。

names(result) <- paste0('binded_', test$Name)
list2env(result, .GlobalEnv)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM