AutomatedDataCollectionWithR icon indicating copy to clipboard operation
AutomatedDataCollectionWithR copied to clipboard

练习题的答案也有错误,没有勘误表。

Open realalien opened this issue 8 years ago • 2 comments

比如第四章习题7(g),答案使用的是(f)的代码。这道题有些搞脑子,花了我一两个小时。 毕竟大家是在学习工具;当工具稍微复杂了点,就照葫芦画瓢吧,节省大家时间。

#(g) Extract the <name> node for all presidents whose term started in or after the year 1960.
library(stringr)    
choose <- function(x) {
    start <- xmlValue(xmlChildren(x)[["start"]])
    name <- xmlValue(xmlChildren(x)[["name"]])
    year_pat <- "([0-9]{4}$)"
    year <-   as.numeric( str_extract(start, year_pat ))
    after1960 <- ifelse( year >= 1960  ,  name, NA )
    return(after1960)
}
temp <- xpathSApply(potus, "//document/president", choose)
presidents_after_1960 <-  temp[!is.na(temp)]

realalien avatar Mar 31 '17 07:03 realalien

第四章 第9题

library(XML)
library(stringr)
library(maps)
doc <- xmlParse("cemetery_eg.KML")

# xpathSApply(doc, '//x:Placemark', namespaces= c(x = "http://www.opengis.net/kml/2.2"), process)
# xpathSApply(places, "//*[local-name()= 'placemark']", xmlValue)  # NOTE: gives warning that returns NULL

cemeteries <- do.call(rbind, xpathApply(doc, '//x:Placemark', namespaces= c(x = "http://www.opengis.net/kml/2.2"), function(node) {
    latlng <- xmlValue(xmlChildren(node[["Point"]])[["coordinates"]])
    name <- xmlValue(node[["name"]])
    lng <- as.numeric( sub(",","", str_extract(latlng, "(.*),")))
    lat <- as.numeric( sub(",","", str_extract(latlng, ",(.*)")))
    data.frame(name, lat, lng, stringsAsFactors = FALSE)
}))

## mapping

# NOTE: install.packages("mapdata")  install.packages("maps") 
library(maps) 
library(mapdata) 

#REF: https://www.students.ncl.ac.uk/keith.newman/r/maps-in-r#countries
map('worldHires',
    c('UK', 'Ireland', 'Isle of Man','Isle of Wight', 'Wales:Anglesey'),
    xlim=c(-11,3), ylim=c(49,60.9))
points(cemeteries$lng, cemeteries$lat, col = "red", cex = .05)
box()

realalien avatar Mar 31 '17 14:03 realalien

大家可以参考一下。

coderLMN avatar Sep 11 '17 02:09 coderLMN