将所有因子列转换为相同的因子

时间:2017-06-20 10:51:09

标签: r survey

我有以下调查数据(例子):

 df <- data.frame(
   Q1 = c("1 = very bad", 2, "5 = very good", 4, 3),
   Q2 = c(2, 3, "1 = very bad", "5 = very good", NA),
   Q3 = c(0.5, 2.3, 4.5, 4.6, 2), #The average of Q1 and Q2, which are 
                                  #questions of the same category
   Q4 = c("Strongly disagree", "Neutral", "Agree", "Strongly Agree", 
        "Strongly disagree")
)

但是有大约350个问题和500.000个答案。为了总结所有答案,我希望所有因子列都以

的形式出现
  as.factor(c(1:5))

我发现了

 levels(df$Q1) <- as.factor(c(1:5))

完成这项工作。但是,有超过300列,我不想手动执行,我似乎无法为所有列进行此操作。我正在搜索apply种函数,该函数首先将列识别为因子,然后将其级别更改为1:5

非常感谢任何帮助,提前谢谢!

1 个答案:

答案 0 :(得分:2)

我们可以为factor列创建索引,使用lapply遍历数据集子集,指定levels

i1 <- sapply(df, is.factor)
df[i1] <- lapply(df[i1], factor, levels = 1:5)