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

后端 未结 6 1585
情深已故
情深已故 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:31

    Use cbind e.g.

    df <- data.frame(b = runif(6), c = rnorm(6))
    cbind(a = 0, df)
    

    giving:

    > cbind(a = 0, df)
      a         b          c
    1 0 0.5437436 -0.1374967
    2 0 0.5634469 -1.0777253
    3 0 0.9018029 -0.8749269
    4 0 0.1649184 -0.4720979
    5 0 0.6992595  0.6219001
    6 0 0.6907937 -1.7416569
    

提交回复
热议问题