简体   繁体   English

R:按名称范围对数据框列进行索引

[英]R: Index data frame columns by ranges of their names

I have a large number of huge data frames. 我有大量的巨大数据帧。 Often in these data frames I have groups of columns with similar names that appear sequentially. 通常在这些数据框中,我会按顺序出现具有相似名称的列组。 The following is a simplified version of such data frame: 以下是此类数据框的简化版本:

> tmp <- data.frame(ID = 1:25,
    Item1 = sample(x = 1:4, size = 25, replace = TRUE),
    Item2 = sample(x = 1:4, size = 25, replace = TRUE),
    Item3 = sample(x = 1:4, size = 25, replace = TRUE),
    Item4 = sample(x = 1:4, size = 25, replace = TRUE),
    Item5 = sample(x = 1:4, size = 25, replace = TRUE),
    Item6 = sample(x = 1:4, size = 25, replace = TRUE),
    Item7 = sample(x = 1:4, size = 25, replace = TRUE),
    Quest = rep(x = 20, times = 25))

I need to find a way to index these columns by ranges of their names, not by their positions. 我需要找到一种通过这些列的名称范围而不是它们的位置来索引这些列的方法。 Say I need to index columns from Item4 to Item7 . 说我需要将列从Item4索引到Item7 I could do the following: 我可以执行以下操作:

> tmp[ , c("Item4", "Item5", "Item6", "Item7")]

Not quite nice when you have hundreds of columns with similar names. 当您有数百个名称相似的列时,效果会不太好。 I would like to do something like: 我想做类似的事情:

> tmp[ , c("Item4":"Item7")]

But it throws an error: 但这会引发错误:

Error in "Item1":"Item7" : NA/NaN argument
In addition: Warning messages:
1: In `[.data.frame`(tmp, , c("Item1":"Item7")) :
  NAs introduced by coercion
2: In `[.data.frame`(tmp, , c("Item1":"Item7")) :
  NAs introduced by coercion

Further, I'd like to use this kind of indexing to manipulate, say, columns' attributes in a way as (using the former approach listing all column names) 此外,我想使用这种索引来操作列的属性,例如(使用列出所有列名称的前一种方法)

> labels.Item4to7 <- c("Disagree", "Somewhat disagree",
  "Somewhat agree", "Agree")
> tmp[ , c("Item4", "Item5", "Item6", "Item7")] <- lapply(tmp[ , c("Item4",
  "Item5", "Item6", "Item7")], factor, labels = labels.Item4to7)

But defining ranges of column names as Item4:Item7 . 但是将列名的范围定义为Item4:Item7

Thank you in advance. 先感谢您。

您可以使用paste

tmp[, paste0("Item", 4:7)]

Use function which 使用功能

tmp[,which(names(tmp)=="Item4"):which(names(tmp)=="Item7")]

Changing the values of items 4 to 7 could be achieved with the following: 可以通过以下操作将项目4的值更改为:

labels.Item4to7 <- c("Disagree", "Somewhat disagree",
  "Somewhat agree", "Agree")
tmp[,which(names(tmp)=="Item4"):which(names(tmp)=="Item7")]<-
   lapply(tmp[,which(names(tmp)=="Item4"):which(names(tmp)=="Item7")],
   factor,labels=labels.Item4to7)

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

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