hopefully this is not a too dumb question, but still being an R beginner I have a serious problem with tapply. Lets say
factors <- as.factor( c("a", "b", "c", "a", "b", "c", "a", "b", "c") )
values <- c( 1, 2, 3, 4, 5, NA, 7, NA, NA )
tapply(
values,
factors,
function(x){
if( sum(is.na(x)) == 1 ){
x[ is.na(x) ] <- 0
}
return(x)
}
)
The result is
$a
[1] 1 4 7
$b
[1] 2 5 0
$c
[1] 3 NA NA
However, what I need is to get a vector back which preserves the original order of values, i.e.:
c( 1,2,3,4,5,NA,7,0,NA )
Many thanks in advance.