提问人:Jake L 提问时间:12/20/2021 最后编辑:AndrewGBJake L 更新时间:12/20/2021 访问量:524
一次透视多组列
pivot multiple sets of columns at once
问:
我有一个数据框,我在其中回归了一堆数据集,然后获取了这些数据集的子集并再次回归。这导致了一个数据帧,其中的列显示“完整”数据集的斜率、截距和标准误差,然后显示“子集”数据集的这些内容的更多列。
我想将数据集转换为长格式,其中一列显示它是哪种类型(完整或子集),然后其他列显示斜率、se、截距等。
我已经找到了一种方法,通过执行许多不同的时间,然后过滤到透视创建的新列匹配的位置,但这并不是最好的方法。我想知道是否有办法在透视函数中列出列集以跳过这一大块代码。reprex在下面。pivot_longer
# made dataframe
df <-
tribble(
~trial, ~full_slope, ~full_slope_se, ~subset_slope, ~subset_slope_se,
1, 10, 1, 12, 2.5,
2, 9, 1.2, 8.5, 3,
3, 9.5, 2, 9.9, 3
)
# pivot
df %>%
# first pivot the slope columns
pivot_longer(cols = c(full_slope, subset_slope),
names_to = "type",
values_to = "slope") %>%
# next pivot the SE columns
pivot_longer(cols = c(full_slope_se, subset_slope_se),
names_to = "type_se",
values_to = "se") %>%
# add a column for when they match up (slope and se both from same dataset, full or subset)
mutate(
data_type =
case_when(
type == "full_slope" & type_se == "full_slope_se" ~ "full",
type == "subset_slope" & type_se == "subset_slope_se" ~ "subset"
)) %>%
# remove rows that are musmatched
filter(!is.na(data_type)) %>%
# remove extra columns made by pivots
select(-type, -type_se) %>%
relocate(data_type, .after = trial)
这确实给了我想要的输出,我只是觉得这不能是我应该这样做的方式。提前致谢!
答:
3赞
r2evans
12/20/2021
#1
用:names_pattern=
df %>%
pivot_longer(-trial, names_pattern = "([^_]*)_(.*)", names_to = c("data_table", ".value"))
# # A tibble: 6 x 4
# trial data_table slope slope_se
# <dbl> <chr> <dbl> <dbl>
# 1 1 full 10 1
# 2 1 subset 12 2.5
# 3 2 full 9 1.2
# 4 2 subset 8.5 3
# 5 3 full 9.5 2
# 6 3 subset 9.9 3
评论