跨数据框将李克特数据转换为数值 [英] Converting Likert Data to Numeric Across A Data Frame

查看:46
本文介绍了跨数据框将李克特数据转换为数值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据集,其中有90个对李克特项目的响应,我想将其转换为数值.它的结构类似于此处的示例:

I have a dataset with 90 responses to Likert Items that I would like to convert to numeric values. It is structured like the example here:

q6 <- c("Daily", "Never", "Often", "Very Often", "Daily")
q7 <- c("Never", "Never", "Often", "Often", "Daily")
q23 <- c("Daily", "Often", "Never", "Never", "Neutral")
q17 <- c("Important", "Important", "Very Important", "Neutral", "Not Important")
example <- cbind(q6, q7, q17, q23)

对每个问题的回答略有不同,但是主要是在强烈不同意"到强烈同意",每天"到从不"或重要到不重要"的范围内.对90个问题的每个回答都在单独的列中(标记为q1> q90).我想用与文本响应(强同意(3)到强烈不同意(-3),通过中立(0))相对应的数值为响应集创建新列.像这样

The responses to each question differ slightly, but are in the main either in the range of Strongly Disagree to Strongly Agree, Daily to Never, or Important to Not Important. Each of the responses to the 90 questions are in a separate column (labelled q1 > q90). I'd like to create new columns for set of responses with a numeric value that corresponds to the text response (Strong Agree (3) to Strongly Disagree (-3), via Neutral (0)). Like so

q6 <- c("Daily", "Never", "Often", "Very Often", "Daily")
n6 <- c(3,-3,1,2,3)
q17 <- c("Important", "Important", "Very Important", "Neutral", "Not Important")
n17 <- c(2,2,3,0,-3)
num_example <- cbind(q6, n6, q17, n17)
num_example

到目前为止,我已经设法用下面的代码生成了一个名为n6的新变量,该变量与现有q6列中的文本响应匹配,然后可以使用cbind将其添加到现有数据框中.我的问题是:我如何在90个问题的整个数据框架中实现自动化,而不必为每个响应运行下面的代码(即将q6更改为q7,然后更改为q8,依此类推).

I've managed to get so far with the code below, which generates a new variable called n6 that matches the text responses in the existing q6 column, that I can then add to the existing data frame using cbind. My questions is: how would I automate this across the entire data frame of 90 questions without having to run the code below for each response (i.e. changing q6 to q7, then to q8, and so on).

n6 <- ifelse(example$q6=="Daily", 3,
                  ifelse(h16$q6=="",0,
                  ifelse(h16$q6=="Very Often", 2,
                  ifelse(h16$q6=="Often", 1,
                  ifelse(h16$q6=="Neither Rarely nor Often", 0,
                  ifelse(h16$q6=="Rarely", -1,
                  ifelse(h16$q6=="Very Rarely", -2,
                  ifelse(h16$q6=="Never", -3,5
                         ))))))))

为进一步参考,按照上面的示例,q6:q12列,然后q23:30列的响应范围从Daily到Never.q17:q22列的响应范围从不重要"到非常重要",q49:q90列的响应范围从非常同意"到非常不同意".我正在尝试找到一种更聪明的方式来在相关列(例如q6:12,q23:q30)上运行以下代码,从而在名为n6:n16,n23:30的列中生成带有数值的新数据框,而不必将代码运行低于90次!

For further reference, columns q6:q12, then q23:30 have responses ranging from Daily to Never, as per the example above. Columns q17:q22 have responses ranging from Not Important to Very Important, Columns q49:q90 have responses that range from Strongly Agree to Strongly Disagree. I'm trying to find a smarter way of running the code below over the relevant columns (e.g. q6:12, q23:q30) in a way that generates a new data frame with numeric values in columns named n6:n16, n23:30, rather than having to run the code below 90 times!

希望这是对该问题的明确解释.

Hope this is a clear explanation of the issue.

谢谢.

推荐答案

如果要使用基数R,我建议使用命名向量来构建查找表,而不是嵌套多个 ifelses s,例如:

If you want to use base R, I would recommend using named vectors to build a look-up table, rather than nesting multiple ifelsess eg:

n <- c('Daily'=3, 'Very Often'=2, 'Often'=1, 'Never'=-3)
n[q6]
#Daily      Never      Often Very Often      Daily 
#    3         -3          1          2          3 
n[q7]
#Never Never Often Often Daily 
#   -3    -3     1     1     3 

这篇关于跨数据框将李克特数据转换为数值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆