R:“过滤器"列表中的数据框中的列 [英] R: "Filter" columns in a data frame by a list

查看:29
本文介绍了R:“过滤器"列表中的数据框中的列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从我现有的数据框中生成一个新的数据框,其中根据该变量是否列在单独的向量中(即作为行)来选择这个新 df 中的列.因此,新的 df 将只包含向量中列出的那些列.为了效率,我想这样做而不必手动指示这些列.

I want to produce a new data frame from my existing one, where the columns in this new df are selected based on whether that variable is listed in a separate vector (i.e., as rows). The new df would therefore only contain those columns that were listed in the vector. I want to do this without having to manually indicate those columns, for efficiency's sake.

我的直觉是,这是一个非常简单的操作,但对 R 来说很陌生,我不确定如何解决这个问题.

My intuition is that this is a pretty simple operation, but being very new to R I'm not exactly sure how to approach the problem.

谢谢!

推荐答案

我今天刚刚使用了这个(以及关于 SO 的另一个答案).

I just used this today (and in another answer on SO).

如果要创建串联列表:

matchingList<-c("a","b","b")

并且您有一个具有一些相同列名的数据框 df,然后您可以像这样对其进行子集:

and you have a data frame df with some of the same column names, then you can subset it like this:

newDF<- df[ ,which((names(df) %in% matchingList)==TRUE)]

如果你用英语从左到右阅读这个代码说明:

If you were to read this left to right in english with instructions the code says:

  • 创建一个名为 newDF 的新数据框
  • 将 newDF 设置为等于数据框所有行的子集 <-df[ ,(行位于逗号之前和括号之后的空格中)
  • 其中 df 中的列名 which((names(df)
  • 与列出的匹配名称进行比较时 %in% matchingList)
  • 返回真值 ==TRUE)
  • create a new data frame named newDF
  • Set newDF equal to the subset of all rows of the data frame <-df[ , (rows live in space before the comma and after the bracket)
  • where the column names in df which((names(df)
  • when compared against the matching names that list %in% matchingList)
  • return a value of true ==TRUE)

它仅对两者中都存在的字段进行子集化,并返回逻辑值 TRUE 以满足比较两个列表的 which 语句.

It subsets only the fields that exist in both and returns a logical value of TRUE to satisfy the which statement that compares the two lists.

还有更简单的方法,但这个方法允许您广泛更改 df 和匹配列表,而不必重新调整过滤器.

There are more brief ways, but this one allows you the change the df and matching list extensively and not have to retool the filter.

这篇关于R:“过滤器"列表中的数据框中的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆