如何在R中提取具有相同名称但标识符不同的列 [英] How to extract columns with same name but different identifiers in R

查看:154
本文介绍了如何在R中提取具有相同名称但标识符不同的列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很抱歉,如果它太基础了,但是我对R不熟悉.

Sorry if it is too basic, but I am not familiar with R.

我有一个包含多个具有相同列名的列的数据框,因此在导入到R之后,已添加了标识符.像这样:

I have a data frame with multiple columns having the same column names, so after being imported to R, identifiers have been added. Something like this:

A = c(2, 3, 5)
A.1 = c('aa', 'bb', 'cc')
A.2 = c(TRUE, FALSE, TRUE) 
B = c(1, 2, 5)
B.1 = c('bb', 'cc', 'dd')
B.2 = c(TRUE, TRUE, TRUE) 

df = data.frame(A, A.1, A.2, B, B.1, B.2) 

df
  A A.1   A.2 B  B.1   B.2
1 2  aa  TRUE 1   bb  TRUE
2 3  bb FALSE 2   cc  TRUE
3 5  cc  TRUE 5   dd  TRUE

我想提取所有具有A的列,而不考虑标识符扩展名,所以它变成:

I would like to extract all columns that have A, regardless of the identifier extension so it becomes like:

  A A.1   A.2 
1 2  aa  TRUE 
2 3  bb FALSE 
3 5  cc  TRUE 

我知道我们可以

df2 = df[, c("A", "A.1", "A.2")]

但是我有很多这种类型的列,所以我不想单独输入.我相信有很聪明的方法可以做到这一点.

But I have many of this type of columns so I do not want to type in individually. I am sure there are smart ways to do this.

谢谢!

推荐答案

尝试执行此操作以获取名称以"A"开头的所有列

Try this to get all the columns with names starting with "A"

df2 = df[, grepl("^A", names( df))]

R的提取'['功能允许在其两个参数模式下使用逻辑索引.您会发现R中的regex函数非常有用,我建议您阅读?regex并在@G上查找SO和Rhelp档案上的示例.格洛腾迪克

R's extraction '['-function allows the use of logical indexing in its two-argument mode. You will find the regex functions in R very useful and may I recommend reading ?regex as well as looking for examples on SO and Rhelp Archives by @G. Grothendieck

这篇关于如何在R中提取具有相同名称但标识符不同的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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