简体   繁体   English

根据 R 中不同数据框中的另一列选择列

[英]Select columns based on another column in a different data frame in R

I have a df:我有一个 df:

AA <- c("GA","GA", "GA","GA","GA")
A <- c(1,2,3,4,5)
B <- c(5,4,3,2,1)
C <- c(2,3,4,5,1)
D <- c(4,3,2,1,5)
df <- data.frame(AA, A, B, C, D)

The other df is:另一个 df 是:

E <- c("B", "D")
F <- c("GA","GA")
df2 <- data.frame(E, F)

I would like to only select the columns from df based on the values from df2$E.我只想根据 df2$E 的值从 df 中选择列。 And that data frame would look like this:该数据框将如下所示:

AA <- c("GA","GA", "GA","GA","GA")
B <- c(5,4,3,2,1)
D <- c(4,3,2,1,5)
df3 <- data.frame(AA, B, D)

My current code below gives me a empty data frame with 0 obs and 5 variables下面我当前的代码为我提供了一个空数据框,其中包含 0 个 obs 和 5 个变量

df3 <- df %>% filter(df %in% df2$E)

Any assistance in generating a code that works would be greatly appreciated.任何生成有效代码的帮助将不胜感激。 Thank you!谢谢!

在这里,我们可以通过列名进行索引。

df[,c("AA",df2$E)]

暂无
暂无

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

相关问题 R-根据一列中跨不同列的公共值,将data.frame格式化为另一个“组合” data.frame - R- format a data.frame into another 'combined' data.frame based on common values within a column dependent across different columns 如何根据另一个数据框的列选择数据框的列 - How to select columns of a data frame based on the columns of another data frame 根据 R 中的列名创建一个包含来自另一个数据框中的列的新数据框 - create a new data frame with columns from another data frame based on column names in R R:根据条件(不同大小的数据框)从另一个数据框的列中为列分配值 - R: Assign values to column, from a column from another data frame, based on a condition (different sized data frames) 如何 select 基于向量的数据框中每一行的不同列 R - How to select different columns for each row in a data frame in R based on vectors R - select 年基于其他列的计算值在数据框中 - R - select years within a data frame based on calculated values from another columns 如何基于一列的部分与另一数据框中的值的匹配来填充R中的列 - How to fill columns in R based on matching parts of one column to values in another data frame 将日期范围内数据框某些列的值相乘,并基于 R 中另一列的值 - Multiply values of some columns of a data frame within a date range and based on the values of another column in R 如何根据同一数据帧中的其他其他列元素从R数据帧中清除列元素? - How do you clear column elements from an R data frame based off of other another columns elements in the same data frame? 基于R中不同列和行选择的数据帧中的数据填充 - Data population in data frame based on different columns and row selection in R
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM