【发布时间】: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