Column binding in R

后端 未结 1 1730
无人及你
无人及你 2020-12-11 14:02

I am using the cbind command in R to bind many data.frames together and each data frame has the same column names so when I bind them all, R automatically changes the column

相关标签:
1条回答
  • 2020-12-11 14:28

    To illustrate the points from my comment:

    > d1 <- data.frame(a = 1:5,b = 1:5)
    > d2 <- data.frame(a = letters[1:5],b = letters[1:5])
    > cbind(d1,d2)
      a b a b
    1 1 1 a a
    2 2 2 b b
    3 3 3 c c
    4 4 4 d d
    5 5 5 e e
    
    > data.frame(cbind(d1,d2))
      a b a.1 b.1
    1 1 1   a   a
    2 2 2   b   b
    3 3 3   c   c
    4 4 4   d   d
    5 5 5   e   e
    
    > x <- data.frame(cbind(d1,d2))
    > sort(colnames(x))
    [1] "a"   "a.1" "b"   "b.1"
    > x[,order(colnames(x))]
      a a.1 b b.1
    1 1   a 1   a
    2 2   b 2   b
    3 3   c 3   c
    4 4   d 4   d
    5 5   e 5   e
    
    0 讨论(0)
提交回复
热议问题