-
Notifications
You must be signed in to change notification settings - Fork 3
/
data-demo.R
97 lines (71 loc) · 2.94 KB
/
data-demo.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# =============================================================================
# title: R Workshop Demo: Reading and Writing Data
# author: Nura Kawa
# summary: A quick demonstration of reading/writing csv, text files.
# Loading data from web.
# data:
# =============================================================================
setwd("data") # change to your data directory
# reading web data
# =============================================================================
setwd("web")
library(RCurl)
library(XML)
library(stringr)
library(plyr)
# Read the URL.
url <- "http://www.geos.ed.ac.uk/~weather/jcmb_ws/"
# Gather the html links present in the webpage.
links <- getHTMLLinks(url)
# Identify only the links which point to the JCMB 2015 files.
filenames <- links[str_detect(links, "JCMB_2015")]
filenames <- filenames[1:5] # for the purpose of saving time with this demo
# Store the file names as a list.
filenames_list <- as.list(filenames)
# Create a function to download the files by passing the URL and filename list.
downloadcsv <- function (mainurl,filename) {
filedetails <- str_c(mainurl,filename)
download.file(filedetails,filename)
}
# Now apply the l_ply function and save the files into the current R working directory.
l_ply(filenames,downloadcsv,mainurl = "http://www.geos.ed.ac.uk/~weather/jcmb_ws/")
# reading CSV files
# =============================================================================
setwd("..")
aug <- read.csv("web/JCMB_2015_Aug.csv",
header = TRUE)
View(aug)
# now, let us create a new .csv and write it. we only want to keep the date,
# time, and rainfall
humidity <- data.frame("date-time" = aug$date.time,
"humidity" = aug$relative.humidity....)
# writing CSV files
# =============================================================================
write.csv(humidity, file="humidity-august.csv")
# reading text files
# =============================================================================
ast <- readLines("ast.txt")
ast
# reading web data
# =============================================================================
setwd("web")
library(RCurl)
library(XML)
library(stringr)
library(plyr)
# Read the URL.
url <- "http://www.geos.ed.ac.uk/~weather/jcmb_ws/"
# Gather the html links present in the webpage.
links <- getHTMLLinks(url)
# Identify only the links which point to the JCMB 2015 files.
filenames <- links[str_detect(links, "JCMB_2015")]
filenames <- filenames[1:5] # for the purpose of saving time with this demo
# Store the file names as a list.
filenames_list <- as.list(filenames)
# Create a function to download the files by passing the URL and filename list.
downloadcsv <- function (mainurl,filename) {
filedetails <- str_c(mainurl,filename)
download.file(filedetails,filename)
}
# Now apply the l_ply function and save the files into the current R working directory.
l_ply(filenames,downloadcsv,mainurl = "http://www.geos.ed.ac.uk/~weather/jcmb_ws/")