What is the best way to transpose a data.frame in R and to set one of the columns to be the header for the new transposed table?

后端 未结 3 1822
南方客
南方客 2021-02-04 01:24

What is the best way to transpose a data.frame in R and to set one of the columns to be the header for the new transposed table? I have coded up a way to do this below. As I am

3条回答
  •  走了就别回头了
    2021-02-04 02:16

    Here are my two cents using dplyr for a data.frame that has grouping columns and an id column.

    id_transpose <- function(df, id){
      df %>% 
        ungroup() %>% 
        select(where(is.numeric)) %>% 
        t() %>% 
        as_tibble() %>% 
        setNames(., df %>% pull({{id}}))
    }
    

提交回复
热议问题