Simplest way to get rbind to ignore column names

后端 未结 2 1683
臣服心动
臣服心动 2020-11-27 18:38

This came up just in an answer to another question here. When you rbind two data frames, it matches columns by name rather than index, which can lead to unexpe

2条回答
  •  隐瞒了意图╮
    2020-11-27 19:29

    You might find setNames handy here...

    rbind(df, setNames(rev(df), names(df)))
    #  x y
    #1 1 3
    #2 2 4
    #3 3 1
    #4 4 2
    

    I suspect your real use-case is somewhat more complex. You can of course reorder columns in the first argument of setNames as you wish, just use names(df) in the second argument, so that the names of the reordered columns match the original.

提交回复
热议问题