Luis F
2009-Oct-21 11:25 UTC
[R] "eliminate" characters of one data.frame col, using another column
Dear Maling list,
I have a data.frame with three columns. I want to produce a fourth
column which is result of "eliminating" the characters present in the
second and third colum from the first.
Example:
a b c
1 ffff f f
2 hhhh j h
3 jjjj g g
I want the result:
ff
hhh
jjjj
I can get what I want using the code below. But it's slow for big files
(which I have) and most likely there's a better way to do this
1) is there a function that would do the same as FUN.remove2ndCol
2) is there a way to avoid the "apply" for every row?
Thanks,
Tiago
###################################
dfIn <- data.frame(a=c('ffff', 'hhhh', 'jjjj'),
b=c('f', 'j', 'g'),
c=c('f', 'h', 'g'))
FUN.remove2ndCol <- function(vec){
vec.sub <- sub(vec['b'], '', vec['a'])
vec.sub <- sub(vec['c'], '', vec.sub)
return(vec.sub)
}
dfIn$Output <- apply(dfIn, 1, FUN.remove2ndCol)
[[alternative HTML version deleted]]
