[R] merge function in R

Don MacQueen macq at llnl.gov
Mon Jun 5 16:27:29 CEST 2006


(See insertions below)

At 8:49 PM -0300 6/2/06, Juan Santiago Ramseyer wrote:
>Em Qui, 2006-06-01 às 05:59 -0700, Ahamarshan jn escreveu:
>>  hi list,
>>
>>  This question must be very basic but I am just 3 days
>>  old to R. 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
>>
>>  Thanks
>>
>hi
>
>testing the following sequence
>
># matrix examples
>x <- seq(from=-1,to=-20,step =-1)
>v <- seq(1:20)
>dim(x) <- c(5,4)
>dim(v) <- c(5,4)
>
>vx <- rep(NA,times=40)
>dim(vx) <- c(5,8)
>
># answer init
>for (i in 1:4) {
>   vx[,2*i-1] <- v[,i]
>   vx[,2*i] <- x[,i]
>}
># answer end

The loop is more complicated than necessary.

vx2 <- matrix(numeric(40),ncol=8)
vx2[,c(1,3,5,7)] <- v
vx2[,c(2,4,6,8)] <- x

## or more generally
vx2[ ,seq(1,by=2,len=4)] <- v
vx2[ ,seq(2,by=2,len=4)] <- x

## if the order of columns did not matter then the simplest is
vx3 <- cbind(v,x)

-Don

>Juan Santiago Ramseyer
>Eng. Recursos Hídricos
>
>
>>  ______________________________________________
>>  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
>
>______________________________________________
>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


-- 
--------------------------------------
Don MacQueen
Environmental Protection Department
Lawrence Livermore National Laboratory
Livermore, CA, USA



More information about the R-help mailing list