R语言自动拼接多个新添加的列
R language auto Concat the multiple newly added columns
我有以下数据框,我想将项目的列与其数量连接起来。具有通用名称或品牌名称的新项目每天添加为新列及其数量。我想自动检测 R 语言中的所有项目及其数量,并希望连接到提到的预期输出中。
id date item_>>1_generic item_>>1_brand item_>>2_generic item_>>2_brand quantity_>>1_daily quantity_>>2_daily
z1 2022-02-28 NA name1 name11 NA 10 20
z1 2021-10-31 name2 NA name21 NA 10m 20m
z2 2021-12-31 NA name3 name31 NA 20mg 20
r3 2021-10-31 name4 NA name41 NA 40 50
r4 2021-06-30 NA name5 NA name51 50 60
r5 2021-08-31 name6 NA NA name61 10 30
我的预期输出是:
id date item1_quanity item2_quanity
z1 2022-02-28 name1_10 name11_20
z1 2021-10-31 name2_10m name21_20m
z2 2021-12-31 name3_20mg name31_20
r3 2021-10-31 name4_40 name41_50
r4 2021-06-30 name5_50 name51_60
r5 2021-08-31 name6_10 name61_30
df %>%
rename_with(~str_remove(., '>>')) %>%
pivot_longer(-c(id, date), names_to = c('item','num', '.value'),
names_sep = '_') %>%
group_by(id, date, num) %>%
mutate(generic = coalesce(generic, brand),
daily = na.omit(daily)) %>%
filter(!is.na(generic)) %>%
mutate(generic = str_c(generic, daily, sep = '_')) %>%
select(-brand, -daily) %>%
pivot_wider(names_from = c(item, num), values_from = generic,)
# A tibble: 6 x 4
# Groups: id, date [6]
id date item_1 item_2
<chr> <chr> <chr> <chr>
1 z1 2022-02-28 name1_10 name11_20
2 z1 2021-10-31 name2_10m name21_20m
3 z2 2021-12-31 name3_20mg name31_20
4 r3 2021-10-31 name4_40 name41_50
5 r4 2021-06-30 name5_50 name51_60
6 r5 2021-08-31 name6_10 name61_30
我有以下数据框,我想将项目的列与其数量连接起来。具有通用名称或品牌名称的新项目每天添加为新列及其数量。我想自动检测 R 语言中的所有项目及其数量,并希望连接到提到的预期输出中。
id date item_>>1_generic item_>>1_brand item_>>2_generic item_>>2_brand quantity_>>1_daily quantity_>>2_daily
z1 2022-02-28 NA name1 name11 NA 10 20
z1 2021-10-31 name2 NA name21 NA 10m 20m
z2 2021-12-31 NA name3 name31 NA 20mg 20
r3 2021-10-31 name4 NA name41 NA 40 50
r4 2021-06-30 NA name5 NA name51 50 60
r5 2021-08-31 name6 NA NA name61 10 30
我的预期输出是:
id date item1_quanity item2_quanity
z1 2022-02-28 name1_10 name11_20
z1 2021-10-31 name2_10m name21_20m
z2 2021-12-31 name3_20mg name31_20
r3 2021-10-31 name4_40 name41_50
r4 2021-06-30 name5_50 name51_60
r5 2021-08-31 name6_10 name61_30
df %>%
rename_with(~str_remove(., '>>')) %>%
pivot_longer(-c(id, date), names_to = c('item','num', '.value'),
names_sep = '_') %>%
group_by(id, date, num) %>%
mutate(generic = coalesce(generic, brand),
daily = na.omit(daily)) %>%
filter(!is.na(generic)) %>%
mutate(generic = str_c(generic, daily, sep = '_')) %>%
select(-brand, -daily) %>%
pivot_wider(names_from = c(item, num), values_from = generic,)
# A tibble: 6 x 4
# Groups: id, date [6]
id date item_1 item_2
<chr> <chr> <chr> <chr>
1 z1 2022-02-28 name1_10 name11_20
2 z1 2021-10-31 name2_10m name21_20m
3 z2 2021-12-31 name3_20mg name31_20
4 r3 2021-10-31 name4_40 name41_50
5 r4 2021-06-30 name5_50 name51_60
6 r5 2021-08-31 name6_10 name61_30