[R] Merging files function

Chuck Cleland ccleland at optonline.net
Thu Jun 1 15:16:48 CEST 2006


Ahamarshan jn wrote:
> hi list,
> 
> This question must be very basic but I am just 3 days
> old to R, so I think i can ask. I am trying to find a
> function to merge two
> tables of data in two different files as one.
> 
>  Does merge function only fills in colums between two
> table where data is missing or is there a way that
> merge can be used to merge data between two matrixes
> with common dimensions.
> 
> say i have
> 
>    v1 v2 v3 v4   
> h1
> h2
> h3
> h4
> h5
> 
> and and another table with 
> 
>    x1 x2 x3 x4
> h1
> h2
> h3
> h4
> h5
> 
> 
> can i merge the data as
> 
>    v1 x1 v2 x2 v3 x3 v4 x4
> h1
> h2
> h3
> h4
> h5

See ?merge.

df1 <- as.data.frame(matrix(rnorm(20), ncol=4))
df2 <- as.data.frame(matrix(rnorm(20), ncol=4))
names(df1) <- paste("v", 1:4, sep="")
names(df2) <- paste("x", 1:4, sep="")
row.names(df1) <- paste("h", 1:5, sep="")
row.names(df2) <- paste("h", 1:5, sep="")

newdf <- merge(df1, df2, by="row.names")

> Thanks
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
> 

-- 
Chuck Cleland, Ph.D.
NDRI, Inc.
71 West 23rd Street, 8th floor
New York, NY 10010
tel: (212) 845-4495 (Tu, Th)
tel: (732) 512-0171 (M, W, F)
fax: (917) 438-0894



More information about the R-help mailing list