I am new to using tidyverse. I want to see if I am being as efficient/concise as possible using the functions in this package. I suspect I am not.
My original data has the key sym as part of each column name.
day a_x b_x a_y b_y
1 1 -0.56047565 1.2240818 -1.0678237 0.42646422
2 2 -0.23017749 0.3598138 -0.2179749 -0.29507148
...
I would like to make the data look tidy, like so:
day sym x y
1 1 a 0.118 0.702
2 2 a -0.947 -0.262
...
11 1 b 1.44 0.788
12 2 b 0.452 0.769
Here is my code that does the above transformations:
library(tidyverse)
set.seed(123)
# example original table
d <- tibble(day=1:10,a_x=rnorm(10),b_x=rnorm(10),a_y=rnorm(10),b_y=rnorm(10))
# manipulations
d1 <- gather(d,a_x,b_x,key='sym',value='x') %>% mutate(sym=sub('_x','',sym)) %>% select(day,sym,x)
d2 <- gather(d,a_y,b_y,key='sym',value='y') %>% mutate(sym=sub('_y','',sym)) %>% select(day,sym,y)
d <- d1 %>% full_join(d2,by=c('day','sym'))
What would be a better way to use some of the tidyverse functions to to achieve the same result in fewer lines or more efficiently?
Thanks!
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…