Reputation: 43
I'm a SPSS user trying to switch to R.
I have several variables in a data frame (ea01
to ea06
; pre01
to pre09
).
Data comes from a questionnaire and is in likert format (integer from 1 to 5).
SPSS has an option to validate the values of these variables, to check if all of them fall within the expected range, and show in which have 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
Validation procedure should tell me that case 1 as an invalid value in variable ea03
and case 3 in variable ea04
.
How can this be done in R?
Upvotes: 3
Views: 220
Reputation: 887128
You could also do:
indx <- which(df >5 | df < 1,arr.ind=TRUE)
setNames(indx[,1], names(df)[indx[,2]])
#ea03 ea04
#1 3
Upvotes: 5
Reputation: 92292
A quick solution would be (assuming your data called df
)
lapply(df, function(x) which(!x %in% seq_len(5)))
Or if you want to create a custom function, could 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 as in
Validfunc(df)
# $ea03
# [1] 1
#
# $ea04
# [1] 3
Another option will be "melting" the data and subseting 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
Upvotes: 4