[R] reading data from a web
David L Carlson
dcarlson at tamu.edu
Wed Oct 29 21:34:53 CET 2014
You did not read the data with the commands you provided since c1 is not defined so read.fwf() fails immediately. Here is a solution that works for the link you provided, but would need to be modified for months that do not have 30 days:
> lnk <- "http://www.data.jma.go.jp/gmd/env/data/radiation/data/geppo/201004/DR201004_sap.txt"
> raw <- readLines(lnk) # Read the file as text lines
> raw <- raw[19:48] # Pull out the data
> raw <- substr(raw, 16, nchar(raw)) # Strip the leading blanks
> raw <- gsub(" +", ",", raw) # Replace two or more blanks with a comma
> raw <- gsub("\\.\\.\\.", "NA", raw) # Replace ... with NA
> Solar <- read.csv(text=raw, header=FALSE, colClasses=c("character",
+ rep("numeric", 25)))
> str(Solar)
'data.frame': 30 obs. of 26 variables:
$ V1 : chr "4 1" "4 2" "4 3" "4 4" ...
$ V2 : num NA NA NA NA NA NA NA NA NA NA ...
$ V3 : num NA NA NA NA NA NA NA NA NA NA ...
$ V4 : num NA NA NA NA NA NA NA NA NA NA ...
$ V5 : num NA NA NA NA NA NA NA NA NA NA ...
$ V6 : num NA NA NA NA NA NA NA NA NA NA ...
$ V7 : num 0 0 0 2 0 8 0 75 2 0 ...
$ V8 : num 0 0 17 133 0 27 36 218 1 1 ...
$ V9 : num 0 98 29 205 0 23 4 280 1 0 ...
$ V10: num 2 190 62 100 0 9 0 310 7 12 ...
$ V11: num 0 237 49 227 86 9 0 321 0 0 ...
$ V12: num 0 303 21 151 177 13 1 304 52 0 ...
$ V13: num 0 286 72 199 131 8 2 320 33 6 ...
$ V14: num 0 318 203 284 30 1 102 285 9 130 ...
$ V15: num 0 314 241 282 10 0 43 286 93 107 ...
$ V16: num 1 270 171 256 6 1 0 272 181 27 ...
$ V17: num 3 190 100 214 34 0 11 255 177 0 ...
$ V18: num 0 89 69 129 24 0 8 205 138 0 ...
$ V19: num 0 7 2 27 2 0 0 80 30 0 ...
$ V20: num 0 0 0 0 0 0 0 0 0 0 ...
$ V21: num NA NA NA NA NA NA NA NA NA NA ...
$ V22: num NA NA NA NA NA NA NA NA NA NA ...
$ V23: num NA NA NA NA NA NA NA NA NA NA ...
$ V24: num NA NA NA NA NA NA NA NA NA NA ...
$ V25: num NA NA NA NA NA NA NA NA NA NA ...
$ V26: num 6 2302 1036 2209 500 ...
-------------------------------------
David L Carlson
Department of Anthropology
Texas A&M University
College Station, TX 77840-4352
-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Alemu Tadesse
Sent: Wednesday, October 29, 2014 2:21 PM
To: r-help at r-project.org
Subject: [R] reading data from a web
Dear All,
I have data of the format shown in the link
http://www.data.jma.go.jp/gmd/env/data/radiation/data/geppo/201004/DR201004_sap.txt
that I need to read. I have downloaded all the data from the link and I
have it on my computer. I used the following script (got it from web) and
was able to read the data. But, it is not in the format that I wanted it to
be. I want it a data frame and clean numbers.
asNumeric <- function(x) as.numeric(as.character(x))
factorsNumeric <- function(data) modifyList(data, lapply(data[,
sapply(data, is.logical)],asNumeric))
data=read.fwf(filename, widths=c(c1),skip=18, header=FALSE)
data$V2<-as.numeric(gsub(" ","", as.character(data$V2) , fixed=TRUE))
f <- factorsNumeric(data)
Any help is appreciated.
Best,
Alemu
[[alternative HTML version deleted]]
______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
More information about the R-help
mailing list