0

Possible Duplicate:
How to join data frames in R (inner, outer, left, right)?
Combine two data frames and remove duplicate columns

How do I add more than one column to a dataframe?

I have an existing dataframe (dataframe.X) that consists of 189 rows and 35 columns. To this, after column 35, I want to add 19 columns which I have in a separate dataframe (dataframe.Y), also containing 189 rows.

If it was just one column (and dataframe.Y would be only one column), I would do this:

dataframe.X$NEW.COLUMN<-dataframe.Y

But that does not work for more than one column. Is there an easy way to do this, without doing it all manually?

Community
  • 1
  • 1
  • 10
    Look at `?cbind` and `?merge` and read an introduction to R. – Roland Nov 26 '12 at 15:17
  • 2
    This seems like more of a stackexchange question. – Patrick S. Forscher Nov 26 '12 at 15:27
  • @gung: Please consider joining me in voting to migrate to SO (via the *off topic* option). This way, with enough community votes, we can migrate it automatically, taking some burden off the moderators in the process. :-) – cardinal Nov 26 '12 at 16:03
  • 3
    @cardinal Shouldn't this question just be closed instead of migrated? I'm sure there are plenty of examples of how to use `merge()` or the functions in `plyr` there...? I'd suggest the OP do a search on SO. – smillig Nov 26 '12 at 16:22
  • Hi, @cardinal, sorry about that. I did just vote to migrate. In general, my policy is to explain the situation to the OP first, & give them a chance to take action (update, respond, etc). I prefer not to vote to close right away before they've had that chance. It's not clear whether this is always the ideal policy, though, Q's like this one probably have little room for being tweaked so as to become on-topic here. – gung - Reinstate Monica Nov 26 '12 at 16:23
  • @smillig: Good points! In the past, I have seen what I thought was a tendency to first migrate to the appropriate site and then close as a duplicate, if warranted. Maybe that is a mistaken impression of mine based on anecdotal observation, though! – cardinal Nov 26 '12 at 17:51
  • @gung: Certainly no apology is necessary and I personally always appreciate your measured and constructive approach to such matters. – cardinal Nov 26 '12 at 17:53

1 Answers1

2

You want the data frame method for cbind():

A <- B <- data.frame(matrix(runif(100), ncol = 10))
names(A) <- LETTERS[1:10]
names(B) <- LETTERS[11:20]

AB <- cbind(A, B)

> head(AB)
          A         B          C          D           E         F          G
1 0.7712438 0.8812705 0.84882438 0.75724291 0.921585941 0.5164942 0.18571224
2 0.0126251 0.1615442 0.60085004 0.05875528 0.253768847 0.2113731 0.87980683
3 0.3084463 0.2632898 0.78474323 0.79381024 0.090883591 0.2370151 0.73951553
4 0.3569714 0.9129827 0.31129223 0.75632920 0.866286356 0.2824447 0.96211833
5 0.8217136 0.9586726 0.87056708 0.93756202 0.001114461 0.5061194 0.06171122
6 0.5402234 0.4114434 0.05434122 0.40361466 0.536001187 0.4158735 0.99494631
           H         I         J         K         L          M          N
1 0.40434088 0.7099546 0.9936925 0.7712438 0.8812705 0.84882438 0.75724291
2 0.20439643 0.2077438 0.5143336 0.0126251 0.1615442 0.60085004 0.05875528
3 0.83729465 0.1859405 0.7801276 0.3084463 0.2632898 0.78474323 0.79381024
4 0.06617131 0.7731577 0.1581497 0.3569714 0.9129827 0.31129223 0.75632920
5 0.58580980 0.4463787 0.1484671 0.8217136 0.9586726 0.87056708 0.93756202
6 0.30236632 0.3962285 0.8330804 0.5402234 0.4114434 0.05434122 0.40361466
            O         P          Q          R         S         T
1 0.921585941 0.5164942 0.18571224 0.40434088 0.7099546 0.9936925
2 0.253768847 0.2113731 0.87980683 0.20439643 0.2077438 0.5143336
3 0.090883591 0.2370151 0.73951553 0.83729465 0.1859405 0.7801276
4 0.866286356 0.2824447 0.96211833 0.06617131 0.7731577 0.1581497
5 0.001114461 0.5061194 0.06171122 0.58580980 0.4463787 0.1484671
6 0.536001187 0.4158735 0.99494631 0.30236632 0.3962285 0.8330804
Gavin Simpson
  • 170,508
  • 25
  • 396
  • 453