[R] Replacing NAs with 0 for variables with the same ending

Olivier CROUZET olivier.crouzet at univ-nantes.fr
Fri Jan 15 14:32:09 CET 2010



Uli Kleinwechter a écrit :

>>
>> data[,grep("x",names(data))][is.na(data[,grep("x",names(data))])]<-0
>>
> 
> thanks a lot. I'm just afraid that grep matches any occurence of "x" in
> the variable name. So variables which would contain "x" at any position,
> not necessarily only at the last one would be selected, as well. To
> refine my example (sorry for having been so imprecise before...):

Replacing "x" with "x$" in grep calls should do the job...

Olivier.



More information about the R-help mailing list