简体   繁体   English

R中的循环分析

[英]Loop analysis in R

I am trying to do some analysis on several variables. 我正在尝试对几个变量进行一些分析。 I have a dataset called "data". 我有一个名为“数据”的数据集。 Thanks to someone's suggestion here I have managed to make histograms in a loop like this: 由于这里有人的建议,我设法像这样循环制作直方图:

my_vars <- c("Var1","Var2","Var3","Var4") # I have changed the names of the variables for "privacy"

par(mfcol=c(1,1))
for(v in my_vars) {
  hist(data[[v]], probability = TRUE, main=v, xlab="")
  lines(density(data[[v]]), col=2)
}

Now, I am trying to do the same thing with some analysis like this: 现在,我试图通过以下分析来做同样的事情:

for(v in my_vars) {
  shapRes= shapiro.test(data[[v]])
  shapRes
}

However, when I run this latest part this is all I get in the console: 但是,当我运行此最新部分时,这就是我在控制台中得到的全部内容:

> for(v in my_vars) {
+   shapRes= shapiro.test(data[[v]])
+   shapRes
+ }
> 

If I open "shapRes" this is what I see: 如果我打开“ shapRes”,这就是我看到的内容:

Shapiro-Wilk normality test Shapiro-Wilk正态性检验

data: data[[v]] 数据:数据[[v]]
W = 0.9561, p-value = 0.09105 W = 0.9561,p值= 0.09105

I think that this is just the result of the last one. 我认为这只是最后一个的结果。 Does anybody know how can I fix this? 有人知道我该如何解决吗?

Thank you in advance for your time. 预先感谢您的宝贵时间。

You need to store each shapRes. 您需要存储每个shapRes。 I would recommend to use a list to store them: 我建议使用列表来存储它们:

mylist <- list()

for(v in my_vars) {
  shapRes= shapiro.test(data[[v]])
  mylist[[v]] <- shapRes
}

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

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