Skip to content
Prev 206420 / 398503 Next

Replacing NAs with 0 for variables with the same ending

Jim Lemon schrieb:
Hi Jim,

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...):
,ay=c(5,NA,2,NA,0) , xy=c(4,2,NA,2,1))
ax bx ay xy
1  1  3  5  4
2  2  5 NA  2
3  3 NA  2 NA
4 NA  5 NA  2
5  0  1  0  1

The task, again, would be to replace NA's with 0 in "ax" and "bx", but
not in "ay" and "xy"

Best,

Uli