简体   繁体   English

根据R中另一个数据帧内的列从数据帧中删除变量

[英]remove variables from a data frame based on a column within another data frame in R

I want to remove a number of columns from df1 我想从df1中删除许多列

  | A      | B      | C      | D
  | ------ | ------ | ------ | ------
1 | 0.870  | 0.435  | 0.968  | 0.679
2 | 0.456  | 0.259  | 0.906  | 0.467
3 | 0.298  | 0.256  | 0.457  | 0.768
4 | 0.994  | 0.987  | 0.365  | 0.765

if they appear as values within a column called TEST within df2 如果它们显示为df2中名为TEST的列中的值

  | TEST   |  
  | ------ | 
1 | A      | 
2 | B      | 
df1[,!(colnames(df1) %in% df2$TEST)]

对我有用的评论的答案是:

newDF = df1[, -which(colnames(df1) %in% df2$TEST)] 

我们也可以使用setdiff

 df1[setdiff(names(df1), df2$TEST)]

暂无
暂无

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

相关问题 根据组和另一个数据帧删除R数据帧中的行 - remove rows in R data frame based on group and another data frame R:根据文件名列从数据框中删除行 - R: Remove rows from data frame based on file name column R:根据来自另一个数据框的匹配行更新列 - R: Update column based on matching rows from another data frame R:从一个数据框中提取行,基于列名匹配另一个数据框中的值 - R: Extract Rows from One Data Frame, Based on Column Names Matching Values from Another Data Frame R:基于来自另一数据帧的条件对数据帧进行子集化 - R: subset a data frame based on conditions from another data frame 根据 R 中的列名创建一个包含来自另一个数据框中的列的新数据框 - create a new data frame with columns from another data frame based on column names in R 根据另一个数据框中的列值从 R 中的数据框中删除行 - Delete rows from a data frame in R based on column values in another data frame 对于R中的data.frame,根据来自另一个数据帧的值从一个数据帧提取数据 - For data.frame in R, pulling data from one data frame based on values from another data frame 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 根据数据框R中的另一列从另一列中提取一列的值 - Extract values for a column from another column based on another column in data frame R
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM