Last active
August 19, 2021 14:45
-
-
Save rhilfi/88dbe72681163d837f9fa47b08aeb6de to your computer and use it in GitHub Desktop.
pivot_longer_several_columns_names_pattern_numbers #R
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(tidyverse) | |
# thanks for the help: | |
# https://community.rstudio.com/t/pivot-longer-on-multiple-column-sets-pairs/43958/9 | |
# https://cran.r-project.org/web/packages/stringr/vignettes/regular-expressions.html | |
walking_speed_1<-c(0.3,0.5,0.7) | |
balance_four_balance_test_1<-c(1,2,1) | |
tug_1<-c(10,20,12) | |
walking_speed_2<-c(0.4,0.6,0.8) | |
balance_four_balance_test_2<-c(2,3,2) | |
tug_2<-c(11,21,12) | |
walking_speed_3<-c(0.5,0.7,0.9) | |
balance_four_balance_test_3<-c(3,4,3) | |
tug_3<-c(12,22,12) | |
data_wide<-data.frame(walking_speed_1, walking_speed_2, walking_speed_3, | |
balance_four_balance_test_1, balance_four_balance_test_2, balance_four_balance_test_3, | |
tug_1, tug_2, tug_3) | |
data_long<-data_wide %>% | |
pivot_longer( | |
cols=everything(), | |
names_to=c(".value", "timepoint"), | |
names_pattern="(\\D+)_(\\d)") | |
## with id | |
data_wide<-data_wide %>% | |
mutate(id=row_number()) %>% | |
select(id, everything()) | |
data_long<-data_wide %>% | |
pivot_longer( | |
cols=-id, | |
names_to=c(".value", "timepoint"), | |
names_pattern="(\\D+)_(\\d)") | |
# if you want to make it really long | |
data_long_long<-data_wide %>% | |
pivot_longer( | |
cols=-id, | |
names_to=c("test", "timepoint"), | |
names_pattern="(\\D+)_(\\d)") | |
## example with three parts: | |
1] "id" "function_t0_control" "function_t0_intervention" "function_t1_control" "function_t1_intervention" | |
[6] "function_t2_control" "function_t2_intervention" "function_t3_control" "function_t3_intervention" | |
data_long<-data %>% | |
pivot_longer(cols=-id, | |
names_to=c(".timepoint", "group"), | |
names_pattern="[function_t]_(.*)_(\\D+)") | |
[1] "id" "timepoint" "group" "value" | |
## example with several variables | |
names(data) | |
[1] "function_t0.control" "function_t0.intervention" "function_t1.control" "function_t1.intervention" "function_t2.control" "function_t2.intervention" "function_t3.control" "function_t3.intervention" | |
data_long1<-data %>% | |
pivot_longer(everything(), | |
names_to = c('.value', 'group'), | |
names_sep = '\\.') | |
names(data_long1) | |
[1] "group" "function_t0" "function_t1" "function_t2" "function_t3" | |
*** | |
id<-1:100 | |
no_beer.t0<-rnorm(100, 50, 12) | |
lost_beer.t0<-rnorm(100, 40, 12) | |
developed_beer.t0<-rnorm(100, 30,12) | |
always_beer.t0<-rnorm(100,20,5) | |
data<-data.frame(id, no_beer.t0, lost_beer.t0, developed_beer.t0, always_beer.t0) | |
data<-data %>% | |
mutate(no_beer.t1=no_beer.t0+rnorm(100, 20,12), | |
lost_beer.t1=lost_beer.t0+rnorm(100,20,12), | |
developed_beer.t1=developed_beer.t0+rnorm(100,10,12), | |
always_beer.t1=always_beer.t0+rnorm(100,20,12)) | |
data_long<-data %>% | |
pivot_longer(cols=-id, | |
names_to=c("group", "time"), | |
names_sep = "\\.", | |
values_to="pain" | |
) | |
ggplot(data_long, aes(x=time, y=pain, color=group))+ | |
geom_boxplot() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment