简体   繁体   English

不明白我在反向编码方面做错了什么?

[英]Don't understand what I'm doing wrong with reverse coding?

I'm trying to reverse code some of my data, as 24 of the columns are negatively keyed and needs to be reversed.我正在尝试对我的一些数据进行反向编码,因为其中 24 列是负键并且需要反转。

I use this dataset: https://www.kaggle.com/tunguz/big-five-personality-test This is the background documentation for the data, and explains how the different questions are keyed: https://ipip.ori.org/New_IPIP-50-item-scale.htm When I try to use the reverse code from psych I just get this error:我使用这个数据集: https://www.kaggle.com/tunguz/big-five-personality-test这是数据的背景文档,并解释了如何键入不同的问题: https://ipip.ori。 org/New_IPIP-50-item-scale.htm当我尝试使用 psych 的反向代码时,我得到了这个错误:

Error in items %*% keys.d : 
  requires numeric/complex matrix/vector arguments

I also get an error telling me that I can't do it with lists.... for some reason, I can't seem to produce that error now...我也收到一个错误,告诉我我不能用列表来做......由于某种原因,我现在似乎无法产生那个错误......

library("pacman", "psych")
dataIPIP <- data.final[, c(1:50)]

col_names2 <- c(paste("E", 1:10, sep = ""), paste("N", 1:10, sep = ""), paste("A", 1:10, sep = ""), paste("C", 1:10, sep = ""), paste("O", 1:10, sep = ""))
names(dataIPIP) <- col_names2

keys <- c(1,-1,1,-1,1,-1,1,-1,1,-1,
         -1,1,-1,1,-1,-1,-1,-1,-1,-1,
         -1,1,-1,1,-1,1,-1,1,1,1,
          1,-1,1,-1,1,-1,1,-1,1,1,
          1,-1,1,-1,1,-1,1,1,1,1)
spm_rev <- reverse.code(keys, items = dataIPIP[,-1], mini = rep(1,50), maxi = rep(5,50))

The structure for the dataIPIP dataframe is: dataIPIP dataframe 的结构为:

'data.frame':   1015341 obs. of  50 variables:
 $ E1 : chr  "4" "3" "2" "2" ...
 $ E2 : chr  "1" "5" "3" "2" ...
 $ E3 : chr  "5" "3" "4" "2" ...
 $ E4 : chr  "2" "4" "4" "3" ...
 and it goes like this through E5-10, N1-10, A1-10, C1-10 and finally O1-10.

It looks like your data has been read in as character variables (the "chr" in the structure overview of your data).看起来您的数据已作为字符变量读入(数据结构概述中的“chr”)。 R doesn't know how to "reverse code a letter", so you need to change the variables to actual numerical values. R 不知道如何“反转编码字母”,因此您需要将变量更改为实际数值。

The way the dataframe looks on kaggle it should be straight forward to just convert each column in the data frame. dataframe 在 kaggle 上看起来的方式应该很简单,只需转换数据帧中的每一列。

One version to do this in base R would be在基础 R 中执行此操作的一个版本是

dataIPIP <- as.data.frame(sapply(dataIPIP, as.numeric))

Hope that helps.希望有帮助。 Cheers干杯

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

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