R:按变量迭代
我有以下数据集1:
Height | Group
1,556 | A
2,111 | B
1,556 | A
2,341 | B
1,256 | A
2,411 | B
我想通过变量 Group
myvar <- c("Height")
res<- vector("list", length(myvars))
a <- factor(dataset1$Group)
myfactor <- levels(a)
i=1
for (myfactor in dataset1) {
res[[i]] <- shapiro.test(dataset1$Size)
i=i+1
}
res 计算高度的 shapiro wilk 正态性检验 - 返回 n 组测试,但全部具有相同的 p 值和 W。 谁能帮我找出问题所在吗?
I have the following dataset1:
Height | Group
1,556 | A
2,111 | B
1,556 | A
2,341 | B
1,256 | A
2,411 | B
I would like to compute shapiro wilk normality test for Height by variable Group
myvar <- c("Height")
res<- vector("list", length(myvars))
a <- factor(dataset1$Group)
myfactor <- levels(a)
i=1
for (myfactor in dataset1) {
res[[i]] <- shapiro.test(dataset1$Size)
i=i+1
}
res - returns n groups of tests, but all with same p-value and W.
Can anyone help me figure out what's wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
编写新代码比查找代码中的所有错误更容易。
It is easier to write new code than find all errors in your code.
您的代码可以通过各种方式进行处理。以下是一些:
myfactor
,然后将其设为迭代器。dataset1
是您的数据(data.frame?)。我什至不确定for (myfactor in dataset1)
创建的循环内将包含什么myfactor
。shapiro.test
的数据进行子集化。myvars
未定义,dataset1$Size
可能应该是dataset1$Height
。试试这个吧。
Your code is hosed is all sorts of ways. Here are a few:
myfactor
outside of the loop, but then you make it the iterator.dataset1
is your data (data.frame?). I'm not even sure whatmyfactor
will be inside a loop created byfor (myfactor in dataset1)
.shapiro.test
.myvars
isn't defined anddataset1$Size
should probably bedataset1$Height
.Try this instead.
谢谢您的回复。
未来通知:
如果您希望按因子计算(对于数据集中选定的变量)正态性检验:
PS:上一个示例中的 sex = GROUP
再次感谢
希望这段代码有助于减少代码
M。
Thanks for the reply.
For future notice:
If you wish to compute (for selected variables in a dataset) a normality test by factor:
PS: sex = GROUP in the previous example
Again Thanks
Wish this code helps reducing code
M.