且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在 data.frame 的两列之间添加(插入)一列

更新时间:2022-12-11 21:24:34

我建议你使用 tibble 包.

I would suggest you to use the function add_column() from the tibble package.

library(tibble)
dataset <- data.frame(a = 1:5, b = 2:6, c=3:7)
add_column(dataset, d = 4:8, .after = 2)

请注意,您可以使用列名代替列索引:

Note that you can use column names instead of column index :

add_column(dataset, d = 4:8, .after = "b")

如果更方便,或者使用参数 .before 而不是 .after.

Or use argument .before instead of .after if more convenient.

add_column(dataset, d = 4:8, .before = "c")