[R] Concatenating data frames in R versus SAS

Nordlund, Dan (DSHS/RDA) NordlDJ at dshs.wa.gov
Fri Aug 24 01:38:08 CEST 2012


> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of ramoss
> Sent: Thursday, August 23, 2012 1:55 PM
> To: r-help at r-project.org
> Subject: [R] Concatenating data frames in R versus SAS
> 
> I am trying to concatenate 2 datasets that don't have exactly the same
> column.
> In SAS I did: data summary;
>   set agency prop;
> run;
> No problem
> in R I get error message
> summary <-rbind(agency,prop)
> Error in match.names(clabs, names(xi)) :
>   names do not match previous names
> 
> But when I use rbin.fill, that overwrites the second file w/ first one.
> Is there a way to replicate the sas process in R?
> 
> Thanks in advance
> 
> 

There may be a more R-ish way to do this, but here is a function to get you started.  

SASrbind <- function(x, y) {
  x[,setdiff(names(y),names(x))] <- NA
  y[,setdiff(names(x),names(y))] <- NA
  rbind(x,y)
  }

wanted <- SASrbind(agency,prop)


Hope this is helpful,

Dan

Daniel J. Nordlund
Washington State Department of Social and Health Services
Planning, Performance, and Accountability
Research and Data Analysis Division
Olympia, WA 98504-5204




More information about the R-help mailing list