I have a dataframe for a likert scale questionnarie. All of the questions have a 1-10 scale, but to run some analyses I want to recode the scale to be from 1 to 5.
So, I want to recode most of the variables of my data frame, excluding identification variables. I've tried several things and looked also into past threads but I cannot find a solution, I always get an error.
Since I'm new to R I believe it is just a rookie mistake but I hope you can help me.
Here is the code:
z <- data.frame (ID = c(23,24,25,26,27),
Project = c("EA","EA","PLA","PLA","PLA"),
Q1 = c(3,9,8,5,10),
Q2 = c(1,2,6,7,9),
Q3 = c(4,8,6,6,10))
recode_z <- z[,3:5]
p3[,recode_p3] <- as.data.frame(lapply(p3[, recode_p3], function(x)ifelse(x == 2, 1),
ifelse(x == 3, 2),
ifelse(x== 4, 2),
ifelse(x== 5, 3),
ifelse(x== 6, 3),
ifelse(x== 7, 4),
ifelse(x== 8, 4),
ifelse(x== 9, 5),
ifelse(x== 10, 5,x)))
I get this error:
Error in .subset(x, j) : invalid subscript type 'list'
Can you spot the mistake? Thank you in advance!
Noting that the values are integers between 2 and 10 and that
(2:10) %/% 2
#[1] 1 1 2 2 3 3 4 4 5
therefore the new values are given by
(2:10 + 1) %/% 2
#[1] 1 2 2 3 3 4 4 5 5
here is a one-liner.
recode_z <- 3:5
z[recode_z] <- lapply(z[recode_z], function(x) (x + 1L) %/% 2L)
z
# ID Project Q1 Q2 Q3
#1 23 EA 2 1 2
#2 24 EA 5 1 4
#3 25 PLA 4 3 3
#4 26 PLA 3 4 3
#5 27 PLA 5 5 5
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다