Home > Mobile >  R - Stacking column while keeping information of another column
R - Stacking column while keeping information of another column

Time:11-18

I have a dataframe of this form:

| Group | Author.1 | Author.2 | Author.3 | Year |
| ------| ------- | ------- | ------- | -----|
| Name1 | J. M Tale      |S. D. Uythy | L.I Trop | 2008|
| Name2 | K. V. Marc     |J. M Tale | T.Y Tapez | 2008 |
| Name3 | J. M Tale     | | | 2010|
| Name4 | S. D. Uythy   | | | 2010|

The columns go up to Author.19.

I would like to create a stacked dataset of the form:

| Group | Author | Year |
| ----- | ------ | ---- |
| Name1 | J. M Tale | 2008 |
| Name2 | J. M Tale  | 2008 |
| Name3 | J. M Tale  | 2010 |
| Name1 | S. D. Uythy   | 2008 |
| Name1 | L.I Trop   | 2008 |

So basically appending Author.2 and Author.3 below Author.1, but keeping the "year" column information.

Any idea how I could do that?

Thanks!

CodePudding user response:

You can use pivot.longer from tidyr package

library(tidyr)
df %>% 
  pivot_longer(-c(Group, Year), values_to = "Author") %>% 
  select(-name) %>% 
  mutate(Author = trimws(Author))
# A tibble: 12 × 3
   Group      Year Author       
   <chr>     <dbl> <chr>        
 1 " Name1 "  2008 "J. M Tale"  
 2 " Name1 "  2008 "S. D. Uythy"
 3 " Name1 "  2008 "L.I Trop"   
 4 " Name2 "  2008 "K. V. Marc" 
 5 " Name2 "  2008 "J. M Tale"  
 6 " Name2 "  2008 "T.Y Tapez"  
 7 " Name3 "  2010 "J. M Tale"  
 8 " Name3 "  2010 ""           
 9 " Name3 "  2010 ""           
10 " Name4 "  2010 "S. D. Uythy"
11 " Name4 "  2010 ""           
12 " Name4 "  2010 ""
  • Related