Home > OS >  cbind two data.frame and specify its rownames
cbind two data.frame and specify its rownames

Time:02-15

I have two dataframes like this:

d <- data.frame(a=c(12,14,15), b=c("a", "b", "c"))
# > d
#    a b
# 1 12 a
# 2 14 b
# 3 15 c

d2 <- data.frame(num=c(1, 2, 3),
                date=as.Date(c("20210111", "20220122", "20220302"),
                             format="%Y%m%d",
                             origin="1900-01-01"),
                relation=as.factor(c("father", "mather", "brother")),
                chara=c("hello", "world", "again"),
                row.names=c("one", "two", "three"))

# > d2
#       num       date relation chara
# one     1 2021-01-11   father hello
# two     2 2022-01-22   mather world
# three   3 2022-03-02  brother again


And I want to cbind the two data.frames but I can not specify its rownames to one of the dataframe. Its rownames is always "a, b, c", which is the rownames of d2.

> cbind(d, d2)
       a b num       date relation chara
one   12 a   1 2021-01-11   father hello
two   14 b   2 2022-01-22   mather world
three 15 c   3 2022-03-02  brother again
> cbind(d2, d)
      num       date relation chara  a b
one     1 2021-01-11   father hello 12 a
two     2 2022-01-22   mather world 14 b
three   3 2022-03-02  brother again 15 c

I checked the help file, and it said "For cbind row names are taken from the first argument with appropriate names: " Of course if I want to reset the rownames, I can add a parameter row.names=NULL like this

> cbind(d2, d, row.names=NULL)
  num       date relation chara  a b
1   1 2021-01-11   father hello 12 a
2   2 2022-01-22   mather world 14 b
3   3 2022-03-02  brother again 15 c

But I am still curious why I can not exchange the order of d and d2 to specify the rownames.

CodePudding user response:

For cbind row names are taken from the first argument with appropriate names

It means that the output rows take the names of the first data frame with row names specified by the user. In your case, d has not been specified row names, so cbind will use that of d2 whether it's in the first or second place in the function.

d <- data.frame(a=c(12,14,15), b=c("a", "b", "c"))
d2 <- data.frame(num=c(1, 2, 3),
                 date=as.Date(c("20210111", "20220122", "20220302"),
                              format="%Y%m%d",
                              origin="1900-01-01"),
                 relation=as.factor(c("father", "mather", "brother")),
                 chara=c("hello", "world", "again"),
                 row.names=c("one", "two", "three"))


cbind(d, d2)
#        a b num       date relation chara
# one   12 a   1 2021-01-11   father hello
# two   14 b   2 2022-01-22   mather world
# three 15 c   3 2022-03-02  brother again

rownames(d) <- rownames(d)
cbind(d, d2)
#    a b num       date relation chara
# 1 12 a   1 2021-01-11   father hello
# 2 14 b   2 2022-01-22   mather world
# 3 15 c   3 2022-03-02  brother again
  •  Tags:  
  • r
  • Related