I can't get a for loop to assign the string 'NA' when none of the other conditions are met.
This is what I've tried...
Height <- c(1.6,3.4,0.42,n/a, 0.5,n/a,1.5,0,n/a,22.0)
Height <- matrix(Height)
h_cat <- matrix(, nrow = length(Height), ncol = 1)
for (i in 1:length(Height)){
if (Height[i]==0)
h_cat[i] <- 'NA'
if (Height[i]>0 & Height[i]<2)
print(Height[i])
h_cat[i] <- '0-2 m'
#print(h_cat[i])
if (Height[i]>=2 & Height[i]<5)
h_cat[i] <- '2-5 m'
if (Height[i]>=5 & Height[i]<10)
h_cat[i] <- '5-10 m'
if (Height[i]>=10)
h_cat[i] <- '>10 m'
else
h_cat[i] <- 'NA'
}
I had a go with is.na() but no luck either.
Update
Sorry that was rushed. Data added.