【问题标题】:How to perform an ANOVA test in R with data in this format? [duplicate]如何使用这种格式的数据在 R 中执行 ANOVA 测试? [复制]
【发布时间】:2013-12-11 00:40:03
【问题描述】:

我有点糊涂了。我有以下格式的数据(对不起,它很丑)已导入 R:

Anova 应该这样使用: aov(group~measurement, data = mydata)

但是,我很难让我的数据适合这种格式。任何帮助将不胜感激。

【问题讨论】:

  • 这称为从宽到长的转换。这里有很多关于它的问题;参见例如Reshape data from wide to long?
  • 另外,请仔细阅读this问题。数据的屏幕截图是在 StackOverflow 上获得任何帮助的最可靠方法之一。

标签: r anova


【解决方案1】:

您可能首先从您拥有的 CSV 文件中读取数据:

df <- read.csv("FLOCKSIZES.csv")

以下示例显示了如何以所需的形式获取数据。请注意,此示例在开始时会生成随机数据,因为到目前为止您还没有提供任何数据。如果您要像上面的示例一样使用read.csv,您可以跳过第一部分并立即从连接测量值的位置继续:

# Some random data similar to what you'd get from read.csv.
numSamples <- 50
df <- data.frame(time=1:numSamples, 
                 group1=rnorm(numSamples),
                 group2=rnorm(numSamples),
                 group3=rnorm(numSamples))

# Concatenate all measurements.
measurements <- c(df$group1, df$group2, df$group3)

# Create a vector that encodes the corresponding group (1 to 3) for each measurement.
# Note: Here we assume that we have the same number of samples from every group.
groups <- do.call(c, lapply(1:3, function(i) rep(i, numSamples)))

# So that you get an idea of what these vectors look like:
print(measurements)
print(groups)

# And here we go:
aov(groups ~ measurements)

【讨论】:

    【解决方案2】:

    使用 csv 包读取 R 中的数据,并使用 reshape2 包中的 melt 函数将其从宽格式转换为长格式

    【讨论】:

    • 这是评论,不是答案。
    猜你喜欢
    • 2021-06-14
    • 1970-01-01
    • 2020-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-08
    相关资源
    最近更新 更多