How to add new column to an dataframe (to the front not end)?

后端 未结 6 1593
情深已故
情深已故 2020-11-30 23:55

How to add a new variable to an existing data frame, but I want to add to the front not end. eg. my dataframe is

b c d
1 2 3
1 2 3
1 2 3

I

6条回答
  •  天涯浪人
    2020-12-01 00:50

    If you want to do it in a tidyverse manner, try add_column from tibble, which allows you to specifiy where to place the new column with .before or .after parameter:

    library(tibble)
    
    df <- data.frame(b = c(1, 1, 1), c = c(2, 2, 2), d = c(3, 3, 3))
    add_column(df, a = 0, .before = 1)
    
    #   a b c d
    # 1 0 1 2 3
    # 2 0 1 2 3
    # 3 0 1 2 3
    

提交回复
热议问题