I hope this is not a too stupid question, but still being a R newbie, I have a serious problem. Let's 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) } )
Result
$a [1] 1 4 7 $b [1] 2 5 0 $c [1] 3 NA NA
However, I need to get back a vector that preserves the original order of values, i.e.
c( 1,2,3,4,5,NA,7,0,NA )
Thank you very much in advance.
source share