【问题标题】:Convert from compass direcitions to degrees r从指南针方向转换为度数 r
【发布时间】:2017-03-04 14:49:39
【问题描述】:

我有一个风向列表,我想将它们转换为度数:

dput(head(w,4))
structure(list(Date = structure(c(6L, 6L, 6L, 6L), .Label = c("01/11/2016", 
"02/11/2016", "03/11/2016", "04/11/2016", "05/11/2016", "06/10/2016", 
"06/11/2016", "07/10/2016", "07/11/2016", "08/10/2016", "08/11/2016", 
"09/10/2016", "09/11/2016", "10/10/2016", "10/11/2016", "11/10/2016", 
"11/11/2016", "12/10/2016", "12/11/2016", "13/10/2016", "13/11/2016", 
"14/10/2016", "14/11/2016", "15/10/2016", "15/11/2016", "16/10/2016", 
"16/11/2016", "17/10/2016", "17/11/2016", "18/10/2016", "19/10/2016", 
"20/10/2016", "21/10/2016", "22/10/2016", "23/10/2016", "24/10/2016", 
"25/10/2016", "26/10/2016", "27/10/2016", "28/10/2016", "29/10/2016", 
"30/10/2016", "31/10/2016"), class = "factor"), Time = structure(76:79, .Label = c("00:00.0", 
"00:08.0", "00:16.0", "00:24.0", "00:32.0", "00:40.0", "00:48.0", 
"00:56.0", "01:04.0", "01:12.0", "01:20.0", "01:28.0", "01:36.0", 
"01:44.0", "01:52.0", "02:00.0", "02:08.0", "02:16.0", "02:24.0", 
"02:32.0", "02:40.0", "02:48.0", "02:56.0", "03:04.0", "03:12.0", 
"03:20.0", "03:28.0", "03:36.0", "03:44.0", "03:52.0", "04:00.0", 
"04:08.0", "04:16.0", "04:24.0", "04:32.0", "04:40.0", "04:48.0", 
"04:56.0", "05:04.0", "05:12.0", "05:20.0", "05:28.0", "05:36.0", 
"05:44.0", "05:52.0", "06:00.0", "06:08.0", "06:16.0", "06:24.0", 
"06:32.0", "06:40.0", "06:48.0", "06:56.0", "07:04.0", "07:12.0", 
"07:20.0", "07:28.0", "07:36.0", "07:44.0", "07:52.0", "08:00.0", 
"08:08.0", "08:16.0", "08:24.0", "08:32.0", "08:40.0", "08:48.0", 
"08:56.0", "09:04.0", "09:12.0", "09:20.0", "09:28.0", "09:36.0", 
"09:44.0", "09:52.0", "10:00.0", "10:08.0", "10:16.0", "10:24.0", 
"10:32.0", "10:40.0", "10:48.0", "10:56.0", "11:04.0", "11:12.0", 
"11:20.0", "11:28.0", "11:36.0", "11:44.0", "11:52.0", "12:00.0", 
"12:08.0", "12:16.0", "12:24.0", "12:32.0", "12:40.0", "12:48.0", 
"12:56.0", "13:04.0", "13:12.0", "13:20.0", "13:28.0", "13:36.0", 
"13:44.0", "13:52.0", "14:00.0", "14:08.0", "14:16.0", "14:24.0", 
"14:32.0", "14:40.0", "14:48.0", "14:56.0", "15:04.0", "15:12.0", 
"15:20.0", "15:28.0", "15:36.0", "15:44.0", "15:52.0", "16:00.0", 
"16:08.0", "16:16.0", "16:24.0", "16:32.0", "16:40.0", "16:48.0", 
"16:56.0", "17:04.0", "17:12.0", "17:20.0", "17:28.0", "17:36.0", 
"17:44.0", "17:52.0", "18:00.0", "18:08.0", "18:16.0", "18:24.0", 
"18:32.0", "18:40.0", "18:48.0", "18:56.0", "19:04.0", "19:12.0", 
"19:20.0", "19:28.0", "19:36.0", "19:44.0", "19:52.0", "20:00.0", 
"20:08.0", "20:16.0", "20:24.0", "20:32.0", "20:40.0", "20:48.0", 
"20:56.0", "21:04.0", "21:12.0", "21:20.0", "21:28.0", "21:36.0", 
"21:44.0", "21:52.0", "22:00.0", "22:08.0", "22:16.0", "22:24.0", 
"22:32.0", "22:40.0", "22:48.0", "22:56.0", "23:04.0", "23:12.0", 
"23:20.0", "23:28.0", "23:36.0", "23:44.0", "23:52.0"), class = "factor"), 
    WindDirection = structure(c(3L, 3L, 3L, 3L), .Label = c("East", 
    "North", "North-east", "South", "South-east", "South-west", 
    "West"), class = "factor"), RainMessage = structure(c(1L, 
    1L, 1L, 1L), .Label = c("Off", "On"), class = "factor"), 
    LightIntensity = c(8.8, 8, 8.3, 12.3), WindSpeed = c(2.1, 
    3, 2.7, 2.4), Temperature = c(10.2, 10.3, 10.4, 10.6)), .Names = c("Date", 
"Time", "WindDirection", "RainMessage", "LightIntensity", "WindSpeed", 
"Temperature"), row.names = c(NA, 4L), class = "data.frame")

我找到了很多相反方向的答案,但从来没有一个可以转换成度数。

我认为它会类似于以下内容,但想知道是否有更优雅的方法:

w$Angle<-NA
if (w$WindDirection=="North") {
  w[,"Angle"]=0
} else if (w$WindDirection=="North-east") {
  w[,"Angle"]=45
} else if (w$WindDirection=="South-east") {
  w[,"Angle"]=135
} else if (w$WindDirection=="South") {
  w[,"Angle"]=180
} else if (w$WindDirection=="South-west") {
  w[,"Angle"]=225
} else
  w[,"Angle"]=NA

【问题讨论】:

    标签: r


    【解决方案1】:

    您可以使用查找

    # look-up: name directions with the equivalent degree
    dir <- setNames( seq(0, 315 , by=45),
                  c("North", "North-east", "East" ,
                    "South-east", "South", "South-west", 
                    "West", "North-west"))
    

    例子

    vec = sample(  c("North", "North-east", "East" ,
        "South-east", "South", "South-west", 
        "West", "North-west"), 25, TRUE)
    

    通过子集获取度数

    dir[vec]
    

    所以在你的例子中使用

    dir[as.character(w$WindDirection)]
    

    **注意从因子到字符的转换


    为了更明确地显示正在发生的事情

    nms <-  c("North", "North-east", "East" ,
        "South-east", "South", "South-west", 
        "West", "North-west")
    dir <- seq(0, 315 , by=45)
    
    # find indices of matches
    mt <- match(vec, nms) 
    
    # Extract values of dir at indexes using subset / `[`
    dir[mt]
    

    如果你有所有 16 个指南针方向,下面是代码:

    dir <- setNames( seq(0, 337.5 , by=22.5), 
                     c("N","NNE","NE", "ENE", "E", "ESE", "SE", "SSE", 
                       "S", "SSW", "SW", "WSW", "W", "WNW", "NW", "NNW"))
    

    【讨论】:

    • 谢谢,这看起来比 for 循环好多了。你能解释一下 dir[vec] 的真正作用吗?我正在努力理解它?
    • 它有点匹配vec 中找到的字符(方向)与dir 的名称(类似于使用数字从向量中提取值的方式,即dir[1])发现实际值是从dir中提取的(使用子集机制)。
    • 如果您拥有所有 16 个指南针方向,请查看以下代码:dir &lt;- setNames( seq(0, 337.5 , by=22.5), c("N","NNE","NE", "ENE", "E", "ESE", "SE", "SSE", "S", "SSW", "SW", "WSW", "W", "WNW", "NW", "NNW"))
    猜你喜欢
    • 1970-01-01
    • 2012-12-27
    • 2011-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-07
    相关资源
    最近更新 更多