[R] alternating element in url
David Winsemius
dwinsemius at comcast.net
Tue Feb 7 06:23:17 CET 2012
On Feb 6, 2012, at 8:05 PM, frauke wrote:
> Hey everybody,
>
> it would be really appreciate if you knew the solution to the
> following
> problem.
>
> I am trying to load data from the internet into R without having to
> save
> them. However, I need to acess a different website for each dataset.
> So I
> want to alternate an element in the URL. Eventually, I want to fill
> a 3
> dimensional matrix/array where each slice is a two-dimensional
> matrix from
> the corresponding website. For example:
>
> ###############################################################################
> ID=c(1,5,6,8,10) #ID of dataset/website, each
> dataset
> contains a matrix [4x6]
> data=array(numeric(0),length(ID) ,4,6) #empty array [5x4x6]
?array ## with attention to the dim argument
>
> for (kk in 1:length(ID)) {
> url <-cat("http://ministry.gov/case",ID[kk],"_agencyk") #should
> return
> "http://ministry.gov/case1_agencyk" for kk=1, etc.
> import.data <- as.matrix( read.table(url) )
>
> for (tt in 1:4) {
>
> for (mm in 1:6) {
> data[kk,tt,mm] <- import.data[tt,mm]
> }}}
> ################################################################################
>
> I got all to work, but the problem is assembling the URL. In the
> code above
> url just returns NULL.
Right. `cat` always returns NULL. If you want to paste things
together, then ... use `paste`
> Once that is solved I have trouble because
> read.table needs the URL to be in "". If I put read.table("url") R
> wont
> recognize it as the url I defined earlier.
You do not want quotes around url. And its a bad idea to give it the
name of a function as well. Can't really offer tested code because http://ministry.gov/case1_agencyk
doesn't exist.
--
David.
> But I also do not know how to use
> cat() to get " at beginning and end.
>
> If I am approaching this in a totally dorky way (likely...), please
> let me
> know, too.
>
> Thank you!! Frauke
>
> Frauke Hoss
> PhD student Carnegie Mellon
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/alternating-element-in-url-tp4363423p4363423.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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.
David Winsemius, MD
West Hartford, CT
More information about the R-help
mailing list