gpt4 book ai didi

重新排列 ANOVA 的数据

转载 作者:行者123 更新时间:2023-12-04 22:25:14 24 4
gpt4 key购买 nike

我还没有完全了解 R 以及如何重新排列数据。我有一个需要重新排列的旧 SPSS 数据文件,以便我可以在 R 中进行方差分析。

我当前的数据文件具有以下格式:

ONE <- matrix(c(1, 2, 777.75, 609.30, 700.50, 623.45, 701.50, 629.95, 820.06, 651.95,"nofear","nofear"), nr=2,dimnames=list(c("1", "2"), c("SUBJECT","AAYY", "BBYY", "AAZZ", "BBZZ", "XX")))

我需要将其重新排列为:
TWO <- matrix(c(1, 1, 1, 1, 2, 2, 2, 2, 1, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 777.75, 701.5, 700.5, 820.06, 609.3, 629.95, 623.95, 651.95), nr=8, dimnames=list(c("1", "1", "1", "1", "2", "2", "2", "2"), c("SUBJECT","AA", "ZZ", "XX", "RT")))

我确信有一种简单的方法可以做到这一点,而不是手动编码。感谢您的考虑。

最佳答案

这应该这样做。你可以稍微调整一下,但这是一个想法:

library(reshape)
THREE <- melt(as.data.frame(ONE),id=c("SUBJECT","XX"))
THREE$AA <- grepl("AA",THREE$variable)
THREE$ZZ <- grepl("ZZ",THREE$variable)
THREE$variable <- NULL

# cleanup
THREE$XX <- as.factor(THREE$XX)
THREE$AA <- as.numeric(THREE$AA)
THREE$ZZ <- as.numeric(THREE$ZZ)

关于重新排列 ANOVA 的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3889065/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com