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

R: Index data frame columns by ranges of their names

我有大量庞大的数据框。通常在这些数据框中,我有一组按顺序出现的具有相似名称的列。以下是此类数据框的简化版本:

> 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))

我需要找到一种方法来按名称范围索引这些列,而不是按位置索引。假设我需要索引从 Item4Item7 的列。我可以执行以下操作:

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

当您有数百个名称相似的列时,这不太好。我想做类似的事情:

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

但是它抛出一个错误:

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

此外,我想使用这种索引来操纵列的属性(使用前一种方法列出所有列名)

> 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)

但是将列名的范围定义为Item4:Item7

提前致谢。

您可以使用 paste

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

使用哪个函数

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

可以通过以下方式将项目 4 的值更改为 7:

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)