0
votes

I am trying to group some wind direction data to text in a new column of a data frame, following the 16 directions (N, NNE, NE etc), but I can't seem to work out an error message.

This is the code:

  RW_Baza[, Aspect_16] <- cut(RW_Baza$Aspect.grade,
                            breaks = c(-Inf, 11.25, 33.75, 56.25, 78.75, 101.25, 123.75, 146.25, 168.75, 191.25, 213.75, 236.25, 258.75, 281.25, 303.75, 326.25, 348.75, Inf),
                            labels = c("N", "NNE", "NE", "ENE", "E", "ESE", "SE", "SSE", "S", "SSW", "SW", "WSW", "W", "WNW", "NW", "NNW", "N"),
                            right=FALSE)

I get this error message:

In levels<-(*tmp*, value = if (nl == nL) as.character(labels) else paste0(labels, : duplicated levels in factors are deprecated.

1
you have two Ns in your data, which is a no no for factors. Assign it using a temp value first and then substituteChris
what would be the workaround here? change one N factor to, say NN, then replace NN with N in the column? but will that afect the levels?Litwos
Change to "NN", then set all values of "NN" to "N". You should be able to with RW_Baza[Aspect_16 == "NN",] <- "N" That process will change the level of all values of "NN" (17) to "N" (1)Chris
it worked after I put the column name between " ". thank you!Litwos

1 Answers

1
votes

I think you're actually getting a warning because, as Chris points out, you've duplicated the N level in your labels definition.

Something like this might work better for you. It takes any angle greater than or equal to 348.75 and multiplies it by -1, making it fit in the -Inf - 11.25 bucket pointing to North.

dir <- runif(1000, 0, 360)

# dir <- ifelse(dir >= 348.75, dir * -1, dir) # Removed to prevent overwriting data
cut(ifelse(dir >= 348.75, dir * -1, dir),
    breaks = c(-Inf, 11.25, 33.75, 56.25, 78.75, 101.25, 123.75, 146.25, 168.75, 191.25, 213.75, 236.25, 258.75, 281.25, 303.75, 326.25, 348.75),
    labels = c("N", "NNE", "NE", "ENE", "E", "ESE", "SE", "SSE", "S", "SSW", "SW", "WSW", "W", "WNW", "NW", "NNW"),
    right=FALSE)

dir <- c(348.75, 348, 350, 1)
#dir <- ifelse(dir >= 348.75, dir * -1, dir) # Removed to prevent overwriting data
cut(ifelse(dir >= 348.75, dir * -1, dir),
    breaks = c(-Inf, 11.25, 33.75, 56.25, 78.75, 101.25, 123.75, 146.25, 168.75, 191.25, 213.75, 236.25, 258.75, 281.25, 303.75, 326.25, 348.75),
    labels = c("N", "NNE", "NE", "ENE", "E", "ESE", "SE", "SSE", "S", "SSW", "SW", "WSW", "W", "WNW", "NW", "NNW"),
    right=FALSE)