data transformation (gather?)

前端 未结 1 1374
说谎
说谎 2021-01-27 09:37

I have the following tibble:

~id, ~a, ~b, ~c, ~d,
1, 10, 20, 33, 42,
2, 30, 20, 32, 42,
3, 34, 24, 35, 32,
4, 32, 24, 35, 25,
5, 22, 14, 35, 36,
...
)

相关标签:
1条回答
  • 2021-01-27 10:05

    Use ?gather to read more on how to use the gather function.

    library(dplyr)
    library(tidyr)
    
    oldTable %>% select(-c, -d) %>% gather(key = aOrB, value = value, -id)
    

    EDIT: If you want to keep c and d, but not stacked:

        oldTable %>% gather(key = aOrB, value = value, -id, -c, -d)
    

    If you want to keep c and d, and stacked along with a and b:

        oldTable %>% gather(key = aOrBOrCOrd, value = value, -id)
    
    0 讨论(0)
提交回复
热议问题