[R] combine two columns

TEMPL Matthias Matthias.Templ at statistik.gv.at
Tue Nov 29 14:45:14 CET 2005


Hello,

Following should work.

m <- matrix(round(runif(16,0,2)),nrow=2)
colnames(m) <- c("A","A","B","B","C","C","D","D")
m2 <- m #matrix(, nrow=dim(m)[1], ncol=dim(m)[2]/2)
z <- 1
ss <- seq(1,dim(m)[2],2)

for(j in ss){
  for(i in 1:dim(m)[1]){
    m2[i,j] <- substring(m[i,ss[z]] == m[i,ss[z]+1],1,1)
  }
  z <- z + 1
}
m2 <- m2[,ss]
colnames(m2) <- colnames(m)[ss]

When your data set is large, then you should try to code this using
apply.

Best,
Matthias

> 
> Hi,
> 
> I have an R programming problem and I havent found anything 
> in the documentation yet:
> 
> I have a data matrix, in which two neighbouring columns 
> represent replicates of the same experiment, e.g. something like this:
> 
>        A A B B C C
> row1   1 1 1 2 2 2
> row2   1 1 1 1 1 2
> 
> I would like to test, if the values for the two replicates in 
> a row are the same or if they differ and generate a new 
> matrix with the results of the tests, something like this:
> 
>         A B C
> row1    T F T
> row2    T T F
> 
> Any hint will be appreciated!
> 
> Georg
> 
> ______________________________________________
> 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
>




More information about the R-help mailing list