Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
578 views
in Technique[技术] by (71.8m points)

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

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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
#...
#...

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...