I created a function in R...
#### generate scoring function
install.packages("multicon")
library(abind)
library(foreach)
library(multicon)
scoring_RSE<- function(scoreDf){
RSE_items <- data.frame(scoreDf$RSE1,scoreDf$RSE2,scoreDf$RSE3,
scoreDf$RSE4,scoreDf$RSE5,scoreDf$RSE6,
scoreDf$RSE7,scoreDf$RSE8,scoreDf$RSE9,
scoreDf$RSE10)
RSE_score <- composite(RSE_items, R = c(2,5,6,8,9), maxScore = 4)
return(RSE_score)
}
to score a survey.
I created a fake dataset to test it and it works. This dataset reads like a list just as mine does
RSE_test <- data.frame(matrix(nrow=3, ncol=14))
for(i in 1:3) {
RSE_test[i,1:14] = sample(1:4, 14, replace=TRUE)
}
colnames(RSE_test) <- c("col1", "col2", "col3", "col4", paste0("RSE",1:10))
### create a dataframe for all of the reverse items
RSE_test_rev <- RSE_test
#reverse code columns
RSE_test_rev[,paste0("RSE", c(2,5,6,8,9))] <- 5 - RSE_test[,paste0("RSE", c(2,5,6,8,9))]
#For the dataframe, find the mean of each row manually
RSE_Scored_manual <- apply(RSE_test_rev[,paste0("RSE", c(1:10))], 1, function(x) mean(x, na.rm = TRUE))
#test the function we made
RSE_Scored_function <- scoring_RSE(RSE_test)
However, when I plug in an actual dataset, I receive this error...
‘-’ not meaningful for factors‘-’ not meaningful for factors‘-’ not meaningful for factors‘-’ not meaningful for factors‘-’ not meaningful for factors
My data set is an imported CSV file that reads like a list:
df1 <- structure(list(RSE1 = c("1", "1", "3"), RSE2 = c("1",
"2", "3"), RSE3 = c("2", "1", "3"), RSE4 = c("2", "4", "2"), RSE5 = c("2", "4", "2"), RSE6 = c("2", "4", "2"), RSE7 = c("2", "4", "2"), RSE8 = c("2", "4", "2"), RSE9 = c("2", "4", "2"), RSE10 = c("2", "4", "2")),
class = "data.frame", row.names = c(NA, -3L))
I tried converting my own dataset to a numeric but it reformats the dataframe.
surveyResps<-as.numeric(unlist(df1))
or
surveyResps<-unlist(lapply(df1, as.numeric))
Even doing columns individually, I can't even find a mean.
Has anyone received an error like this before or has suggestions?