Check the actual values โ€‹โ€‹of the likert scales in R

I am an SPSS user trying to migrate to R.

I have several variables in a dataframe (from ea01

to ea06

; pre01

to pre09

).

The data comes from a questionnaire and is in a format similar (an integer from 1 to 5).

SPSS has the ability to check the values โ€‹โ€‹of these variables to check if they are within the expected range and to show where there are invalid errors.

id ea01 ea02 ea03 ea04  
1  4    5    6    5  
2  3    2    1    3  
3  3    2    4    0  
4  5    3    4    3

      

The check routine should tell me that case 1 as an invalid value in a variable ea03

and case 3 in a variable ea04

.

How can this be done in R?

+3


source to share


2 answers


A quick solution would be (assuming your data is called df

)

lapply(df, function(x) which(!x %in% seq_len(5)))

      

Or if you want to create a custom function you can try

Validfunc <- function(x){
  l <- lapply(x, function(y) which(!y %in% seq_len(5)))
  Filter(Negate(function(...) length(...) == 0), l)
}

      

And then use it like in



Validfunc(df)

# $ea03
# [1] 1
# 
# $ea04
# [1] 3

      


Another option would be to "melt" the data and undercut it accordingly.

library(data.table)
temp <- melt(setDT(df[-1]))
temp[, which(!value %in% seq_len(5)), variable]
#    variable V1
# 1:     ea03  1
# 2:     ea04  3

      

+3


source


You can also do:



indx <- which(df >5 | df < 1,arr.ind=TRUE)
setNames(indx[,1], names(df)[indx[,2]])
#ea03 ea04 
#1    3 

      

+5


source







All Articles