gpt4 book ai didi

R 每隔一列 reshape 数据框

转载 作者:行者123 更新时间:2023-12-02 09:13:20 24 4
gpt4 key购买 nike

我很难整理一些以奇怪的方式获取的数据......它有一些患者标识符,然后是带有测试日期的列,然后是带有相应测量值的列。但随着时间的推移,它会重复进行相同的测试,并且数据位于后续列中。

数据框是这样的:

df1 <- data.frame(id = c("A","B"),
test1 = c("10-12-16", "12-10-17"),
test1_result = c("20", "3"),
test2 = c("10-01-17", "11-12-17"),
test2_result = c("18", "4"),
test3 = c("12-03-18", "NA"),
test3_result = c("300", "NA"))

我想获得这样的东西:

df2 <- data.frame(id = c("A", "A", "A", "B", "B", "B"),
tests = c("10-12-16", "10-01-17", "12-03-18", "12-10-17", "11-12-17", "NA"),
results = c("20", "18", "300", "3", "4", "NA")
)

我无法找到一种方法来改变它,任何帮助将非常感激。

谢谢!

最佳答案

您可以尝试从data.tablemelt:

library(data.table)
setDT(df1)

df2 <- melt(df1, id = 'id', measure = patterns('test\\d$', '_result'))[
, .(id, tests = value1, results = value2)]

# id tests results
# 1: A 10-12-16 20
# 2: B 12-10-17 3
# 3: A 10-01-17 18
# 4: B 11-12-17 4
# 5: A 12-03-18 300
# 6: B NA NA

关于R 每隔一列 reshape 数据框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49604203/

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