0

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?

Julia
  • 37
  • 5
  • 2
    Julia, as [mentioned](https://stackoverflow.com/q/63747113/3358272#comment112728173_63747113) in your [previous question](https://stackoverflow.com/q/63747113/3358272) using the same apparent dataset: (1) Please do not post an image of code/data/errors ... just include the code, console output, or data (e.g., `dput(head(x))` or `data.frame(...)`) directly. (2) Answering your previous question will likely help solve this problem, as you'll have `numeric` and not `factor`s. (*Really*, the best solution most likely is to fix how you import the data.) – r2evans Sep 04 '20 at 22:22
  • 1
    Can you specify where your error appears (you can run `summary(rlang::last_error())` to get a stack trace that helps debug). However, I'd agree with your intuition that it really seems like the solution should be to convert your data to numeric. Can you add a [reproducible example](https://stackoverflow.com/q/5963269/1335174) (using `dput`) of your data, ideally after you converted it? Also, how did you convert to numeric / can you verify with `str(rse_test)` that this step worked fine and your columns are of numeric type? – alex_jwb90 Sep 04 '20 at 22:27
  • summary(rlang::last_error()) outputs that there is no error as I suppose the output is technically not meaningful for factors. – Julia Sep 04 '20 at 22:51
  • 1
    Problem solved by importing data differently ```survey<-read_csv("~/Downloads/Learning_To_Code_Surveys.csv", col_names = T, col_types = cols(.default = col_integer())) ``` – Julia Sep 04 '20 at 23:26
  • Glad to hear you were able to isolate and fix the problem. – r2evans Sep 05 '20 at 00:33

0 Answers0