简体   繁体   English

R将factor()用于名义数据-如何对多个变量的标签赋值

[英]R Use factor() for nominal data - how to value labels for multiple variables

I'm an R newbie so this problem is probably quite obvious but I have had a good search around and can't find anything. 我是R新手,所以这个问题可能很明显,但是我在附近搜索了很好,找不到任何东西。

I'm wanting to use R to analyse a survey rather than the usual method excel. 我想使用R分析调查,而不是使用常用的Excel方法。

I have my variables labeled as Q1, Q2, Q3... 我将变量标记为Q1,Q2,Q3 ...

Q1 and Q2 contains nominal data (1, 2) and I'd like the values replacing with ("Yes", "No"). Q1和Q2包含标称数据(1、2),我希望将值替换为(“ Yes”,“ No”)。 I can do this for Q1 using the code below but I'm not sure if I should subset or use c( to use the factor function. The survey will have about 25 questions this will need applying to so I'd rather it be done in one line of code rather than 25. 我可以使用下面的代码在Q1上做到这一点,但是我不确定是否应该使用c()来使用因子函数。调查将涉及约25个问题,因此我宁愿这样做用一行代码而不是25行。

resdata$Q1 <- factor(resdata$Q1, levels = c(1,2), labels = c("Yes", "No"))

First you need to define the replacer function: 首先,您需要定义替换函数:

repl.f <- function(x) ifelse(x==1, "Yes","No")

Then simply use this argument in mutate_each() in dplyr: 然后只需在dplyr的mutate_each()中使用此参数即可:

library(dplyr)
resdata <- resdata %>% mutate_each(funs(repl.f),contains("Q"))

Its first argument is the functions you want to apply inside funs(), the second is the subset of columns. 它的第一个参数是要在funs()内部应用的函数,第二个参数是列的子集。 If you only give it one function, then it will by default replace these columns. 如果只给它一个功能,则默认情况下它将替换这些列。 To select the columns you can use another dplyr function:contains(), which much like everything else in dplyr speaks for itself. 要选择列,您可以使用另一个dplyr函数:contains(),就像dplyr中的其他所有内容一样。

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

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