I am trying to extract attributes from an XML but when I try to put them into a dataframe I keep getting errors. I want to use the 'windDirection' and 'windSpeed' data and to have the corresponding 'from' and 'to' time and date stamp that correspond with measurement of wind direction and speed.
library(xml2)
as_list(wind_data <- read_xml("https://metwdb-openaccess.ichec.ie/metno-wdb2ts/locationforecast?lat=51.6547167;long=-9.7734153"))
# Wind direction
direction <- wind_data %>% xml_find_all("//windDirection")
direction %>% xml_attr("deg") %>% as.numeric()
# Wind speed
speed <- wind_data %>% xml_find_all("//windSpeed")
speed %>% xml_attr("mps") %>% as.numeric()
# to and from
time <- wind_data %>% xml_find_all("//time")
time %>% xml_attr("from") %>% as.character()
time %>% xml_attr("to") %>% as.character()
wind_data <- data.frame(direction, speed, time)
I have also tried:
library(XML)
wind_data <- xmlToDataFrame(time, direction, speed)
But I get the following error:
Error in (function (classes, fdef, mtable) : unable to find an inherited method for function ‘xmlToDataFrame’ for signature ‘"xml_nodeset", "xml_nodeset", "xml_nodeset", "missing", "missing"’
I am very new to using XML formats so I could easily be missing something very simple here but I can't seem to find the answer! Any help or pointers would be really appreciated.