简体   繁体   English

具有嵌入向量的心理函数describe()在Var中返回错误

[英]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. 我想使用在其他向量中嵌入的describe()传递向量,以便可以在for循环中使用该函数。 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. 我想获得描述性统计数据,将130位学生分为5个不同的组,因此我为每个组创建一个子集。

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()可以正常工作:

describe(G1A)

However, grouping the subsets into a vector names and passing an index of names to describe() won't work: 但是,将子集分组为向量names ,并将names索引传递给describe()将不起作用:

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? 我该如何运作?

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. 如果调用str(names[1])来查看向量的结构,则会看到为什么它不起作用的原因。

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])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM