R psych::statsBy() error: "'x' must be numeric"

350 views Asked by At

I'm trying to do a multilevel factor analysis using the "psych" package. The first step is recommended to use the statsBy() funtion to have a correlation data:

statsBy(study2, group = "ID")

However, it gives this "Error in FUN(data[x, , drop = FALSE], ...) : 'x' must be numeric".

For the dataset, I only included a grouping variable "ID", and other two numeric variables. I ran the following line to check if the varibales are numeric.

sapply(study2, is.numeric)

 ID     v1      V2 

FALSE TRUE TRUE

Here are the code in the tracedown of the error.But I don't know what 'x' refers here, and I noticed in line 8 and 9, the X is in captital and is lowercase in line 10.

*10. FUN(data[x, , drop = FALSE], ...)

9. FUN(X[[i]], ...)

8. lapply(X = ans[index], FUN = FUN, ...)

7. tapply(seq_len(728L), list(z = c("5edfa35e60122c277654d35b", "5ed69fbc0a53140e516ad4ed", "5d52e8160ebbe900196e252e", "5efa3da57a38f213146c7352", "5ef98f3df4d541726b1bcc48", "5debb7511e806c2a59cad664", "5c28a4530091e40001ca4d00", "5872a0d958ca4c00018ce4fe", "5c87868eddda2d00012add18", "5e80b7427567f07891655e7e", ...

6. eval(substitute(tapply(seq_len(nd), IND, FUNx, simplify = simplify)), data)

5. eval(substitute(tapply(seq_len(nd), IND, FUNx, simplify = simplify)), data)

4. structure(eval(substitute(tapply(seq_len(nd), IND, FUNx, simplify = simplify)), data), call = match.call(), class = "by")

3. by.data.frame(data, z, colMeans, na.rm = na.rm)

2. by(data, z, colMeans, na.rm = na.rm)

1. statsBy(study2, group = "ID")*

The dataset has 728 rows and those like "5edfa35e60122c277654d35b" are IDs. Could anyone help explain what might have gone wrong?

1

There are 1 answers

0
Kyabdro On

I had the same error, the only way was to convert the group variable to the numeric class.

Try:

study2$ID<-as.numeric(study2$ID)
statsBy(study2, group = "ID")

If dat$ID is of class character:

study2$ID<-as.numeric(as.factor(study2$ID))
statsBy(study2, group = "ID")