简体   繁体   English

R 将 psych 组合成一个变量后的两个均值

[英]R package psych two means in one variable after combining

I'm currently analysing data for a student project.我目前正在分析一个学生项目的数据。 During the analysis, I combined two variables into one with cbind():在分析过程中,我用 cbind() 将两个变量合二为一:

interpas$GA02_01 <- cbind(interpas$LP02_01, interpas$ST02_01)

The two variables LP02_01 and ST02_01 are measuring the same questions but for different media-formats.两个变量 LP02_01 和 ST02_01 测量相同的问题,但针对不同的媒体格式。 There's no overlapping between the two.两者之间没有重叠。 The structure is like this:结构是这样的:

LP02_01 ST02_01
1        NA
NA       2
NA       5
4        NA

So they just get combined.所以他们只是结合在一起。 When I calculate the mean with the built-in mean() function from R, I get the mean of the new variable GA02_01.当我使用 R 的内置 mean() 函数计算平均值时,我得到了新变量 GA02_01 的平均值。

But when I'um using the mean function of the package psych, or any other function for descriptive statistics (like describe) from this package, it's calculating the two variables LP02_01 and ST02_01 still seperately.但是,当我使用 psych 包的均值函数或该包中用于描述性统计的任何其他函数(如 describe)时,它仍在分别计算两个变量 LP02_01 和 ST02_01。 Like this:像这样:

> describe(interpas$GA02_01)
   vars   n mean   sd median trimmed  mad min max range skew kurtosis   se
X1    1 151 3.62 1.89      4    3.59 1.48   1   7     6 0.00    -1.24 0.15
X2    2  63 2.70 1.92      2    2.45 1.48   1   7     6 0.85    -0.64 0.24

Does anyone know a solution to this?有谁知道这个问题的解决方案? Unfortunately, I need the descriptive functions skew and kurtosi from the psych package for further analysis and a function to check for normal distribution.不幸的是,我需要 psych 包中的描述性函数 skew 和 kurtosi 进行进一步分析,并需要一个函数来检查正态分布。

Thanks a lot!非常感谢!

I would look into Coalesce我会调查 Coalesce

so you would probably want something like:所以你可能想要这样的东西:

df%>%
mutate(new_var = coalesce(old_var1, old_var2)%>%
select(-c(old_var1,old_var2))

here is the documentation for the function in dplyr.这是 dplyr 中函数的文档。 https://www.rdocumentation.org/packages/dplyr/versions/0.7.8/topics/coalesce https://www.rdocumentation.org/packages/dplyr/versions/0.7.8/topics/coalesce

You just need to unlist your data frame.您只需要unlist您的数据框。 However make sure thatr you are choosing the desired columns in the correct (for your usecase) manner.但是,请确保您以正确的(针对您的用例)方式选择所需的列。 For example, when you use cbind , you create matrix.例如,当您使用cbind ,您将创建矩阵。 You can just use indexing, ie df[1:2] (for first and second columns) or by name, ie df[,c("LP02_01", "ST02_01")] .您可以只使用索引,即df[1:2] (用于第一列和第二列)或按名称,即df[,c("LP02_01", "ST02_01")] That way you result with a data frame object.这样你就会得到一个数据框对象。 Then you can just unlist and describe() , ie然后你可以unlistdescribe() ,即

psych::describe(unlist(interpas[, c("LP02_01", "ST02_01")]))
#   vars n mean   sd median trimmed  mad min max range skew kurtosis   se
#X1    1 4    3 1.83      3       3 2.22   1   5     4    0    -2.24 0.91

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

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