How to add a row names to a data frame in a magrittr chain

前端 未结 4 1076
傲寒
傲寒 2021-01-13 10:49

I want to do the opposite of: Convert row names into first column

Somewhere down the chain of pipes I would like to add row names to the data frame, for example, I w

4条回答
  •  南笙
    南笙 (楼主)
    2021-01-13 11:05

    You can use row.names<-:

    mtcars <- mtcars %>% `row.names<-`(as.character(1:nrow(mtcars)))
    

    Should work. As a demo:

    df <- data.frame(x = 1:5, y = 2:6)
    df <- df %>% `row.names<-`(letters[1:5])
    df
    
    #   x y
    # a 1 2
    # b 2 3
    # c 3 4
    # d 4 5
    # e 5 6
    

提交回复
热议问题