Transposing in dplyr

后端 未结 4 798
故里飘歌
故里飘歌 2021-02-02 14:04

I have the following data.frame

df = structure(list(HEADER = c(\"HOME_TRPM\", \"AWAY_TRPM\", \"HOME_TEAM\",\"AWAY_TEAM\"),
                     price = c(\"0.863         


        
4条回答
  •  悲&欢浪女
    2021-02-02 14:27

    I think you want tidyr rather than dplyr:

    library(tidyr)
    library(dplyr)
    df %>% mutate(group = 1) %>%
           spread(HEADER, price)
    
      group AWAY_TEAM          AWAY_TRPM HOME_TEAM         HOME_TRPM
    1     1       NOP -0.845186446996287       CHA 0.863104076023855
    

    Using this, you can specify your groupings - and you can add on select(-group) to remove them later.

提交回复
热议问题