mardi 12 janvier 2021

ifelse test returns NA when data value is within specified parameters

I'm attempting to apply size classes to trees based on their diameter at breast height (dbh) measurement.

I have the following code to classify them, repeated 19 times.

    data_dbh$size_class_12 = ifelse(data_dbh$dbh1 %in% c(5.0:9.9), "A",
                            ifelse(data_dbh$dbh1 %in% c(10.0:19.9), "B",
                                   ifelse(data_dbh$dbh1 %in% c(20.0:39.9), "C",
                                          ifelse(data_dbh$dbh1 > 39.9, "D", NA))))

Some of the newly created columns in the dataset have NA, when the value in the respective dbh column is within the specified %in% range, such as 14.9, which ought to have returned B; this also occurs in the other %in% ranges.

I've tried str_trim with side = "both" to clear possible spaces, as well as as.numeric and as.character, but nothing changes.

I'm fairly new to R and bad in understanding programming language in general, so if any answers could be simplified as far as possible, that would be great.

Aucun commentaire:

Enregistrer un commentaire