rfor-looppsychdescribe

Psych function describe() with Embedded Vector Returns Error in Var


I'd like to use describe() passing vectors that are embedded in other vectors so that I can use the function in a for loop. I have the following data that contains grades for a group of students. I would like to get descriptive statistics for the 5 different groups the 130 students are sorted into, so I create one subset for each group.

pr3 <- read.csv("data.csv", head = T, sep = ";")

G1A <- subset(pr3, group == "1A")
G1B<- subset(pr3, group == "1B")


Now calling describe() as follows works perfectly:

describe(G1A)

However, grouping the subsets into a vector names and passing an index of names to describe() won't work:

names <- c(G1A, G1B)
describe(names[1])

Error in var(if (is.vector(x) || is.factor(x)) x else as.double(x), na.rm = na.rm) : 
  is.atomic(x) is not TRUE
In addition: Warning message:
In mean.default(x, na.rm = na.rm) :
  argument is not numeric or logical: returning NA
>

How could I make it work?


Solution

  • You can't place your subsets/data frames in a vector and call them in this way.

    If you call str(names[1]) to see the structure of your vector, you will see why it doesn't work.

    Instead you might want to place the two data frames in a list in order to call them with your second line. Like this:

    names <- list(G1A, G1B)
    describe(names[1])