R:按变量迭代

发布于 2024-10-30 21:25:26 字数 470 浏览 1 评论 0原文

我有以下数据集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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

尬尬 2024-11-06 21:25:26

编写新代码比查找代码中的所有错误更容易。

lapply(split(dataset1$Height,dataset1$Group),shapiro.test)

编写新代码比查找代码中的所有错误更容易。

A` Shapiro-Wilk normality test data: X[[1L]] W = 0.75, p-value = 3.031e-08

编写新代码比查找代码中的所有错误更容易。

B` Shapiro-Wilk normality test data: X[[2L]] W = 0.9134, p-value = 0.4295

It is easier to write new code than find all errors in your code.

lapply(split(dataset1$Height,dataset1$Group),shapiro.test)

It is easier to write new code than find all errors in your code.

A` Shapiro-Wilk normality test data: X[[1L]] W = 0.75, p-value = 3.031e-08

It is easier to write new code than find all errors in your code.

B` Shapiro-Wilk normality test data: X[[2L]] W = 0.9134, p-value = 0.4295
衣神在巴黎 2024-11-06 21:25:26

您的代码可以通过各种方式进行处理。以下是一些:

  1. 您在循环外部创建 myfactor,然后将其设为迭代器。
  2. dataset1 是您的数据(data.frame?)。我什至不确定 for (myfactor in dataset1) 创建的循环内将包含什么 myfactor
  3. 您不会对发送到 shapiro.test 的数据进行子集化。
  4. myvars 未定义,dataset1$Size 可能应该是 dataset1$Height

试试这个吧。

res <- list()
for (mf in levels(dataset1$Group)) {
    res[[mf]] <- shapiro.test(dataset1$Height[dataset1$Group == mf])
}

Your code is hosed is all sorts of ways. Here are a few:

  1. You create myfactor outside of the loop, but then you make it the iterator.
  2. dataset1 is your data (data.frame?). I'm not even sure what myfactor will be inside a loop created by for (myfactor in dataset1).
  3. You don't subset the data sent to shapiro.test.
  4. myvars isn't defined and dataset1$Size should probably be dataset1$Height.

Try this instead.

res <- list()
for (mf in levels(dataset1$Group)) {
    res[[mf]] <- shapiro.test(dataset1$Height[dataset1$Group == mf])
}
给妤﹃绝世温柔 2024-11-06 21:25:26

谢谢您的回复。
未来通知:
如果您希望按因子计算(对于数据集中选定的变量)正态性检验:

variaveis <- colnames(dataset1)[c(1:2)]
/////alternative: variaveis <- c("height", "weight") 
res<- vector("list", length(variaveis))

for (i in 1:length(variaveis)) {
    #calcula o shapiro por factor para variaveis selecionadas
    res[[i]] <- lapply(split(dataset1[,variaveis[i]] ,dataset1$sex), shapiro.test)
}
res

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:

variaveis <- colnames(dataset1)[c(1:2)]
/////alternative: variaveis <- c("height", "weight") 
res<- vector("list", length(variaveis))

for (i in 1:length(variaveis)) {
    #calcula o shapiro por factor para variaveis selecionadas
    res[[i]] <- lapply(split(dataset1[,variaveis[i]] ,dataset1$sex), shapiro.test)
}
res

PS: sex = GROUP in the previous example
Again Thanks
Wish this code helps reducing code
M.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文