Home > Net >  How to create a dataframe in R from xml, keep getting error cannot coerce class "xml_nodeset&qu
How to create a dataframe in R from xml, keep getting error cannot coerce class "xml_nodeset&qu

Time:08-17

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.

CodePudding user response:

The issue is the converted results are not stored in a new variable.
Also looking the xml file, One needs to reduce the number of time values in half since there two time nodes for each forecast.

library(xml2)

as_list(wind_data <- read_xml("https://metwdb-openaccess.ichec.ie/metno-wdb2ts/locationforecast?lat=51.6547167;long=-9.7734153"))

# Wind direction and update data
direction <- wind_data %>% xml_find_all("//windDirection")
direction <- direction %>% xml_attr("deg") %>% as.numeric()

# Wind speed and update data
speed <- wind_data %>% xml_find_all("//windSpeed")
speed <-speed %>% xml_attr("mps") %>% as.numeric()

# to and from 
time <- wind_data %>% xml_find_all("//time")
#there are 2 time for each speed and direction
timereduced <- time[ as.logical((1:length(time))%%2) ]
from <- timereduced %>% xml_attr("from") 
to <- timereduced %>% xml_attr("to") 

results <- data.frame(direction, speed, from, to)

Based on Parfait's recommendation:

timenodes <- wind_data %>% xml_find_all(".//time[not(location/precipitation)]")

from <- timenodes %>% xml_attr("from") 
to <- timenodes %>% xml_attr("to") 
direction <- timenodes %>% xml_find_all("//windDirection") %>% xml_attr("deg") %>% as.numeric()
speed <- timenodes %>% xml_find_first(".//windSpeed") %>% xml_attr("mps") %>% as.numeric()

results <- data.frame(direction, speed, from, to)
  • Related