Is there more efficient or concise way to use tidyr::gather to make my data look 'tidy'?

爷,独闯天下 提交于 2021-02-18 16:59:12

问题


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!


回答1:


gather has been retired in favor of pivot_longer which makes such transformation simpler.

tidyr::pivot_longer(d, cols = -day, 
                    names_to = c('sym', '.value'), names_sep = '_')

# A tibble: 20 x 4
#    day sym        x      y
#* <int> <chr>  <dbl>  <dbl>
#1     1 a     -0.560 -1.07 
#2     1 b      1.22   0.426
#3     2 a     -0.230 -0.218
#4     2 b      0.360 -0.295
#...
#...


来源:https://stackoverflow.com/questions/62556300/is-there-more-efficient-or-concise-way-to-use-tidyrgather-to-make-my-data-look

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!