如何在 R 中的一个数据帧中对不同变量进行多次透视?

How to pivot multiple times for different variables in one dataframe in R?

我无法在一个数据集中转换不同的变量。我的原始数据集如下所示:

childid<- c(m1, m2,m3,m4,m5)
begin.age<-c(12,3,NA,4, 5)
end.age<-c(14,5,6,NA, 7)
begin.weight<- c(1,3,NA,4, 5)
end.weight <-c(4,5,7,NA, 8)
phase<-c(1,2,3,4,5)
df<- data.frame(childid, begin.age, end.age, begin.weight, end.weight,phase)

现在,我正在尝试使用 pivot_longer 函数来转换年龄和体重,理想的输出应该是:

childid   timepoint  age weight   phase
m1          begin     12   1       1
m1          end       14   4       1
m2          begin     3    3       3
m2          end       5    5       3
m3          begin     NA   NA      4
m3          end       6    7       4
.....

我尝试将数据集拆分为两个子数据集,并使用 piovt_longer 转换每个变量(年龄、体重),但我无法使用 left_join 将它们成功合并回来。合并的数据集比它应该的多倍。我假设 bc 数据是重复收集的,并且两个数据集都没有唯一的合并键。

df_a<-df%>%
 pivot_longer (
    cols=c(`begin.age`,`end.age`)
    names_to="timepoint",
    values_to="age"

df_w<-df%>%
 pivot_longer (
    cols=c(`begin.weight`,`end.weight`)
    names_to="timepoint",
    values_to="weight"
)

df_p<- df_a%>%
      left_join (df_w, by="childid")

有没有其他更好的方法可以在不拆分 R 中的数据集的情况下实现这一目标?非常感谢~~!

也许这有帮助

library(tidyr)
pivot_longer(df, cols = matches('^(begin|end)'), 
   names_to = c("timepoint", ".value"), 
        names_pattern = "(.*)\.(\w+)$", values_drop_na = TRUE)

数据

df <- structure(list(childid = c("m1", "m2", "m3", NA, "m4"), begin.age = c(12, 
3, NA, 4, 5), end.age = c(14, 5, 6, NA, 7), begin.weight = c(1, 
3, NA, 4, 5), end.weight = c(4, 5, 7, NA, 8), phase = c(1, 2, 
3, 4, 5)), class = "data.frame", row.names = c(NA, -5L))

替代方法:类似于 akrun 的方法,但使用 names_sep 而不是 names_pattern

library(dplyr)
library(tidyr)

df %>% 
  pivot_longer(
    cols = -c(childid, phase),
    names_to = c("timepoint", ".value"), 
    names_sep = "\.",
    values_drop_na = TRUE
  )
  childid phase timepoint   age weight
  <chr>   <dbl> <chr>     <dbl>  <dbl>
1 m1          1 begin        12      1
2 m1          1 end          14      4
3 m2          2 begin         3      3
4 m2          2 end           5      5
5 m3          3 end           6      7
6 m4          4 begin         4      4
7 m5          5 begin         5      5
8 m5          5 end           7      8