coupe=data.frame(read.csv(file.choose()))
#UNITS AS PER GIRTH - I want to assign girth classes to each tree for further computation of value
coupe$g.class = NA
for (i in 1:(nrow(coupe))) {
if (coupe$girth[i] < 60) {
coupe$g.class[i] = 0.25
} else if (coupe$girth[i] < 89) {
coupe$g.class[i] = 0.5
} else if (coupe$girth[i] < 119) {
coupe$g.class[i] = 1
} else if (coupe$girth[i] < 149) {
coupe$g.class[i] = 2
} else if (coupe$girth[i] < 179) {
coupe$g.class[i] = 4
} else if (coupe$girth[i] < Inf) {
coupe$g.class[i] = 6
}
}
Compiling this code gives me the error Error in if (coupe$girth[i] < 60) { : missing value where TRUE/FALSE needed
Any leads on what I am doing wrong? This same block ofcode compiled perfectly on another dataset (different coupe) but isn't doing so for the one I am working with presently

Since your desired
g.classvalues aren't in a specific pattern, you could usefindIntervalto make your cuts, then recode them to your desired values.If you didnt mind your variable being a factor, you could simply do:
If you wanted them as a numeric values, just wrap that in
as.numeric(as.character(...)):You could also do it by
matching on a reference dataframe (here I created two variables,g.classandg.class_ref, just for comparison, but can overwrite instead)Output:
And for a
tidyverseapproach, you could also usedplyr::mutateanddplyr::case_whento recode the values if you didn't want to create a reference data frame/usematch:Data