将数字风向分组到R中的列中的文本

时间:2022-06-18 07:35:49

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.

我试图将一些风向数据分组到数据框的新列中的文本,遵循16个方向(N,NNE,NE等),但我似乎无法找出错误消息。

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.

在级别< - (* tmp *,value = if(nl == nL)as.character(标签)中,否则粘贴0(标签,:不推荐使用因子中的重复级别)。

1 个解决方案

#1


1  

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

我认为你实际上是在收到警告,因为正如Chris指出的那样,你在标签定义中重复了N级。

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.

这样的事情对你来说可能会更好。它需要大于或等于348.75的任何角度并将其乘以-1,使其适合指向North的-Inf - 11.25桶。

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)

#1


1  

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

我认为你实际上是在收到警告,因为正如Chris指出的那样,你在标签定义中重复了N级。

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.

这样的事情对你来说可能会更好。它需要大于或等于348.75的任何角度并将其乘以-1,使其适合指向North的-Inf - 11.25桶。

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)