如何在不结合名称的情况下``pivot_wider`多列''?
我想通过在不合并名称的情况下组合多个列来将pivot_wider
应用于整形的tibble。我的数据包含有关患者药物的信息。给定患者可能会或可能不服用各种药物。这些列包含毒品名称和每日剂量按任意顺序。
问题在于我目前的方法会产生太多的列,因为毒品名称是组合的。请参阅此可重复的示例:
library(tidyverse)
# Let's have data of patients who may or may not take some drugs with a
# corresponding dose:
(
medication <- tribble(
~dob, ~drug_1, ~dose_1, ~drug_2, ~dose_2, ~drug_3, ~dose_3,
"1970-01-01", "Drug A", 100, NA, NA, NA, NA,
"1980-01-01", "Drug B", 150, "Drug A", 200, NA, NA,
"1990-01-01", NA, NA, "Drug C", 500, "Drug B", 100
)
)
# The desired arrangement is as follows:
#
# dob | 'Drug A' | 'Drug B' | 'Drug C'
# -----------|-------------------------------
# 1970-01-01 | 100 | NA | NA
# 1980-01-01 | 200 | 150 | NA
# 1980-01-01 | NA | 100 | 500
# The following attempt to pivot wider creates too many columns by combining all the drug names:
medication %>%
pivot_wider(names_from = starts_with("drug_"),
values_from = starts_with("dose_"))
# # A tibble: 3 × 10
# dob `dose_1_Drug A_NA_NA` `dose_1_Drug B_Drug A_NA` `dose_1_NA_Drug C_Drug B` `dose_2_Drug A_NA_NA` `dose_2_Drug B_Drug A_NA` `dose_2_NA_Drug C_Drug B` `dose_3_Drug A_NA_NA` `dose_3_Drug B_Drug A_NA` `dose_3_NA_Drug C_Drug B`
# <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
# 1 1970-01-01 100 NA NA NA NA NA NA NA NA
# 2 1980-01-01 NA 150 NA NA 200 NA NA NA NA
# 3 1990-01-01 NA NA NA NA NA 500 NA NA 100
我什至尝试应用pivot_wider
函数多次,每次明确说明names_from =“ drug_1”
等等,但这会导致“存在“错误。
如上所示,有没有办法实现所需的布置?谢谢。
I would like to apply pivot_wider
to a tidyverse tibble by combining multiple columns without combining the names. My data contains information about patients' medication. There are various drugs that a given patient may or may not take. The columns contain the drug names and the daily doses in arbitrary order.
The problem is that my current approach produces far too many columns because the drug names are combined. See this reproducible example:
library(tidyverse)
# Let's have data of patients who may or may not take some drugs with a
# corresponding dose:
(
medication <- tribble(
~dob, ~drug_1, ~dose_1, ~drug_2, ~dose_2, ~drug_3, ~dose_3,
"1970-01-01", "Drug A", 100, NA, NA, NA, NA,
"1980-01-01", "Drug B", 150, "Drug A", 200, NA, NA,
"1990-01-01", NA, NA, "Drug C", 500, "Drug B", 100
)
)
# The desired arrangement is as follows:
#
# dob | 'Drug A' | 'Drug B' | 'Drug C'
# -----------|-------------------------------
# 1970-01-01 | 100 | NA | NA
# 1980-01-01 | 200 | 150 | NA
# 1980-01-01 | NA | 100 | 500
# The following attempt to pivot wider creates too many columns by combining all the drug names:
medication %>%
pivot_wider(names_from = starts_with("drug_"),
values_from = starts_with("dose_"))
# # A tibble: 3 × 10
# dob `dose_1_Drug A_NA_NA` `dose_1_Drug B_Drug A_NA` `dose_1_NA_Drug C_Drug B` `dose_2_Drug A_NA_NA` `dose_2_Drug B_Drug A_NA` `dose_2_NA_Drug C_Drug B` `dose_3_Drug A_NA_NA` `dose_3_Drug B_Drug A_NA` `dose_3_NA_Drug C_Drug B`
# <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
# 1 1970-01-01 100 NA NA NA NA NA NA NA NA
# 2 1980-01-01 NA 150 NA NA 200 NA NA NA NA
# 3 1990-01-01 NA NA NA NA NA 500 NA NA 100
I even tried to apply the pivot_wider
function multiple times, each time explicitly stating names_from = "drug_1"
and so on, but this causes a "columns already exist" error.
Is there a way to achieve the desired arrangement as shown above? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
两个步骤,首先旋转更长:
Two steps, pivoting longer first: