我希望将以下因素重新编码为主要风向分量。返回一个错误,我不明白。我真的需要一些帮助来解决这个问题。
变量W.G.Dir为因子形式,有16个水平。
weather$W.G.Dir <- factor(recode(weather$W.G.Dir,
`Southerly` = c(
"SSE", "S",
"SSW"
),
`Easterly` = c("ENE", "E", "ESE"),
`Northerly` = c("NNE", "N", "NNW"),
`Westerly` = c("WNW", "W", "WSW"),
`S'East` = "SE", `S'West` = "SW", `N'East` =
"NE", `N'West` = "NW"
))
错误:Southerly
的长度必须是16或1,而不是3.
因为只有16个点,所以我想创建一个1:1的映射,然后创建一个新的因子来替换旧的因子:
all_points <- c("N", "NNE", "NE", "ENE", "E", "ESE", "SE", "SSE",
"S", "SSW", "SW", "WSW", "W", "WNW", "NW", "NNW")
full <- c("North", "North", "NorthEast", "East", "East",
"East", "SouthEast", "South", "South", "South",
"SouthWest", "West", "West", "West", "NorthWest", "North")
weather$W.G.Dir <- factor(full[match(weather$W.G.Dir, all_points)],
unique(full))
weather
#> W.G.Dir
#> 1 South
#> 2 East
#> 3 SouthEast
#> 4 North
#> 5 North
#> 6 West
#> 7 SouthEast
#> 8 SouthWest
#> 9 West
#> 10 North
数据组成的例子
set.seed(1)
all_points <- c("N", "NNE", "NE", "ENE", "E", "ESE", "SE", "SSE",
"S", "SSW", "SW", "WSW", "W", "WNW", "NW", "NNW")
weather <- data.frame(W.G.Dir = factor(sample(all_points, 10, TRUE), all_points))
weather
#> W.G.Dir
#> 1 S
#> 2 ENE
#> 3 SE
#> 4 N
#> 5 NNE
#> 6 W
#> 7 SE
#> 8 SW
#> 9 WNW
#> 10 NNE