在数据帧内重新编码李克特量表 - 为研究方法学习 R 的建议? [英] Recoding Likert Scales within dataframe -recommendations for learning R for research methodology?

查看:37
本文介绍了在数据帧内重新编码李克特量表 - 为研究方法学习 R 的建议?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近刚开始我的 R 之旅,终于掌握了窍门.我有一个非常简单的问题,但我无法找到我正在寻找的答案.

Just recently began my journey in R and finally getting the hang of things. I have a very simple question yet I'm unable to find the answer I'm looking for.

我有一个使用李克特类型量表收集的调查回复数据集.有些范围从非常强烈不同意到相反 (1-7),而其他范围是 1-5.我正在寻找一种简单的方法来重新编码数据集中的每一列,最好使用 dplyr 包,因为我正在尝试掌握它.到目前为止,我有这个:

I have a dataset of survey responses that were collected using a Likert type scale. Some of the range from very strongly disagree to the opposite (1-7) while others are 1-5. I'm looking for a simple way of recoding each column within the dataset, preferably using the dplyr package as I'm trying to master it. So far, I have this:

df 是我的数据框,其中包含 Q2.1_1 列:Q2.5_1.我需要新的重新编码的列是数字,因为它们当前是因子(我想稍后运行描述).

df is my dataframe which contains columns Q2.1_1: Q2.5_1. I need the new recoded columns to be numeric as they are currently factors (I want to later run descriptives).

这一行的问题在于它创建了一个重新编码的向量,但不在我的 df 数据帧内.我不确定是否应该将其附加到 df 或者是否有更好的方法在 df 本身内进行编辑.

The problem with this line is that it creates a vector that recodes it, but not within my df dataframe. I'm not sure if I should be appending this to df or if there's a better way to just edit within df itself.

as.numeric(recode(df$Q2.1_1, "Very slightly or not at all"=1, .... etc))

推荐答案

我认为您正在寻找类似以下内容的内容:

I think you're looking for something like the following:

mydata <- data.frame(x = c("A lot", "Some", "Not at all"))

mydata <- mutate(mydata, x_recoded = recode(x, "A lot" = 1, "Some" = 2, "Not at all" = 3))

mydata

           x x_recoded
1      A lot         1
2       Some         2
3 Not at all         3

这段代码是:

  1. 创建示例数据
  2. 使用dplyr的mutate()函数创建一个新变量x_recoded
  3. 将变异的数据帧 mydata 分配回自身,使其现在具有 x_recoded
  1. Creating the sample data
  2. Using dplyr's mutate() function to create a new variable x_recoded
  3. Assigning the mutated data frame mydata back to itself so it now has x_recoded in it

recode 推断变量是数字,因为数字在重新编码的右侧,因此您不需要使用 as.numeric.

recode infers that the variable is numeric since numbers are on the right-hand-side in the recodes, so you don't need to use as.numeric.

这篇关于在数据帧内重新编码李克特量表 - 为研究方法学习 R 的建议?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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