gpt4 book ai didi

reshape R 中的数据(宽 -> 长)

转载 作者:行者123 更新时间:2023-12-02 03:17:19 27 4
gpt4 key购买 nike

我想将df1转换成df2

旧示例数据框 df1

df1 <- structure(list(ID = 1:2,                Group = c(1L, 1L),
M1a2hB = c(0.2, 0.3), M1a3hB = c(0.4, 0.6),
M2a2hB = c(0.3, 0.4), M2a3hB = c(0.6, 0.6),
M1r2hB = c(200L, 300L), M1r3hB = c(400L, 600L),
M2r2hB = c(300L, 400L), M2r3hB = c(600L, 600L)),
.Names = c("ID", "Group", "M1a2hB", "M1a3hB", "M2a2hB",
"M2a3hB","M1r2hB", "M1r3hB","M2r2hB", "M2r3hB"),
class = "data.frame", row.names = c(NA, -2L))

ID Group M1a2hB M1a3hB M2a2hB M2a3hB.... M1r2hB M1r3hB M2r2hB M2r3hB ...
1 1 0.2 0.4 0.3 0.6 ... 200 400 300 600 ...
2 1 0.3 0.6 0.4 0.6 ... 300 600 400 600 ...

这里,df1 有 100 个 ID 和 1100 个列。每个结果 measure 都有两列用于绝对变化,两列用于相对变化。有将近 270 个结果 m 措施。

M1a2hB 是第一次测量从时间 2 到基线的绝对变化,M1a3hB 是时间 3 到基线的绝对变化。同样,M1r2hB 是第一个结果从时间 2 到基线的相对变化,M1r3hB 是结果从时间 3 到基线的相对变化。

df2:

ID Group time  M1a           M2a        ...  M1r           M2r        ...
1 1 1 0.0 0.0 ... 000 000 ...
1 1 2 0.2 0.3 ... 200 300 ...
1 1 3 0.4 0.6 ... 400 600 ...
2 1 1 0.0 0.0 ... 000 000 ...
2 1 2 0.3 0.4 ... 300 400 ...
2 1 3 0.6 0.6 ... 600 600 ...

有什么建议吗?随时要求任何澄清。谢谢!期待!

附注我尝试运行以前帖子中的一些代码(如果有兴趣请看下面),但它们看起来不同,因为 df 是三维数据,而 df2 包含额外的时间列

In R, plotting wide form data with ggplot2 or base plot. Is there a way to use ggplot2 without melting wide form data frame?

Reshaping repeated measures data in R wide to long

最佳答案

我们可以使用 sub 从列名中提取模式,split 该向量的序列与 'nm1',将其用作 measuremelt 中将“宽”格式转换为“长”格式。

library(data.table)
nm1 <- sub("\\d+[[:alpha:]]+$", '', names(df1)[-(1:2)])
lst <- split(seq_along(nm1)+2, nm1)
melt(setDT(df1), measure = lst,
value.name= names(lst), variable.name= 'time')[order(ID)]
# ID Group time M1a M1r M2a M2r
#1: 1 1 1 0.2 200 0.3 300
#2: 1 1 2 0.4 400 0.6 600
#3: 2 1 1 0.3 300 0.4 400
#4: 2 1 2 0.6 600 0.6 600

数据

df1 <- structure(list(ID = 1:2, Group = c(1L, 1L),
M1a2hB = c(0.2, 0.3
), M1a3hB = c(0.4, 0.6), M2a2hB = c(0.3, 0.4),
M2a3hB = c(0.6,
0.6), M1r2hB = c(200L, 300L), M1r3hB = c(400L, 600L),
M2r2hB = c(300L,
400L), M2r3hB = c(600L, 600L)), .Names = c("ID", "Group", "M1a2hB",
"M1a3hB", "M2a2hB", "M2a3hB", "M1r2hB", "M1r3hB",
"M2r2hB", "M2r3hB"
), class = "data.frame", row.names = c(NA, -2L))

关于 reshape R 中的数据(宽 -> 长),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35956906/

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