Мы можем использовать pivot_longer
из tidyr
library(dplyr)
library(tidyr)
test1 %>%
pivot_longer(cols = -c(startdate, id), names_to = c('.value', 'grp'), names_sep="_")
Или это может быть
test1 %>%
pivot_longer(cols = -c(startdate, id),
names_to = c( '.value', 'grp'), names_pattern = "^([a-z])(.*)")
# A tibble: 8 x 5
# startdate id grp m f
# <chr> <chr> <chr> <dbl> <dbl>
#1 2019-11-06 POL55 0_9 NA 32
#2 2019-11-06 POL55 10_19 NA NA
#3 2019-11-06 POL55 20_29 NA NA
#4 2019-11-06 POL55 30_39 NA NA
#5 2019-11-06 POL55 40_49 32 NA
#6 2019-11-06 POL55 50_59 NA NA
#7 2019-11-06 POL55 60_69 NA NA
#8 2019-11-06 POL55 70 NA NA
Или может быть
test1 %>%
pivot_longer(cols = -c(startdate, id),
names_to = c( 'grp', '.value'), names_pattern = "^([a-z])(.*)")
# A tibble: 2 x 11
# startdate id grp `0_9` `10_19` `20_29` `30_39` `40_49` `50_59` `60_69` `70`
# <chr> <chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#1 2019-11-06 POL55 m NA NA NA NA 32 NA NA NA
#2 2019-11-06 POL55 f 32 NA NA NA NA NA NA NA
Или это может быть
test1 %>%
pivot_longer(cols = matches("^(f|m)\\d+_?\\d*$"), names_to = 'age_bucket',
values_to = 'count')
# A tibble: 16 x 4
# startdate id age_bucket count
# <chr> <chr> <chr> <dbl>
# 1 2019-11-06 POL55 m0_9 NA
# 2 2019-11-06 POL55 m10_19 NA
# 3 2019-11-06 POL55 m20_29 NA
# 4 2019-11-06 POL55 m30_39 NA
# 5 2019-11-06 POL55 m40_49 32
# 6 2019-11-06 POL55 m50_59 NA
# 7 2019-11-06 POL55 m60_69 NA
# 8 2019-11-06 POL55 m70 NA
# 9 2019-11-06 POL55 f0_9 32
#10 2019-11-06 POL55 f10_19 NA
#11 2019-11-06 POL55 f20_29 NA
#12 2019-11-06 POL55 f30_39 NA
#13 2019-11-06 POL55 f40_49 NA
#14 2019-11-06 POL55 f50_59 NA
#15 2019-11-06 POL55 f60_69 NA
#16 2019-11-06 POL55 f70 NA