How to parse XML to R data frame

前端 未结 4 2121
离开以前
离开以前 2020-11-22 15:51

I tried to parse XML to R data frame, this link helped me a lot:

how to create an R data frame from a xml file

But still I was not able to figure out my prob

4条回答
  •  粉色の甜心
    2020-11-22 16:50

    Use xpath more directly for both performance and clarity.

    time_path <- "//start-valid-time"
    temp_path <- "//temperature[@type='hourly']/value"
    
    df <- data.frame(
        latitude=data[["number(//point/@latitude)"]],
        longitude=data[["number(//point/@longitude)"]],
        start_valid_time=sapply(data[time_path], xmlValue),
        hourly_temperature=as.integer(sapply(data[temp_path], as, "integer"))
    

    leading to

    > head(df, 2)
      latitude longitude          start_valid_time hourly_temperature
    1    29.81    -82.42 2014-02-14T18:00:00-05:00                 60
    2    29.81    -82.42 2014-02-14T19:00:00-05:00                 55
    

提交回复
热议问题