[R] Romoving elements from a vector. Looking for the oppositeof c(), New user

Peter Alspach PAlspach at hortresearch.co.nz
Fri Nov 16 03:36:18 CET 2007


Thomas
 
> Not sure i explained it good enough. Ill try with an example
> 
> say
> 
> x=[3,3,4,4,4,4,5,5,6,8]
> z=[3,4,4,5,5]
> 
> what i want to get after removing z from x is something like 
> x=[3,4,4,6,8]

This will work, but I imagine there are better ways (assuming z is always a subset of x):

z <- c(3,4,4,5,5)
x <- c(3,3,4,4,4,4,5,5,6,8)
tempTab <- merge(table(x), table(z), by='row.names', all=T)
tempTab[is.na(tempTab[,5]),5] <- 0
rep(as.numeric(tempTab[,1]), tempTab[,3]-tempTab[,5])

Peter Alspach



More information about the R-help mailing list