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

iovurdzv  于 2023-03-27  发布在  其他
关注(0)|答案(2)|浏览(136)

我有大量巨大的数据框架。在这些数据框架中,我经常有一组列,它们的名称相似,并按顺序出现。下面是这种数据框架的简化版本:

> 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
先谢谢你。

fsi0uk1n

fsi0uk1n1#

使用的函数

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

unhi4e5o2#

您可以使用paste

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

相关问题