Add (insert) a column between two columns in a data.frame

前端 未结 17 1619
耶瑟儿~
耶瑟儿~ 2020-11-28 02:41

I have a data frame that has columns a, b, and c. I\'d like to add a new column d between b and c.

I know I could just add d at the end by using cbind but h

17条回答
  •  南笙
    南笙 (楼主)
    2020-11-28 03:33

    Here is a an example of how to move a column from last to first position. It combines [ with ncol. I thought it would be useful to have a very short answer here for the busy reader:

    d = mtcars
    d[, c(ncol(d), 1:(ncol(d)-1))] 
    

提交回复
热议问题