Download CSV file using R

Asked

Viewed 865 times

2

  • I edited your question, but I didn’t change its meaning. @Andréblanco

2 answers

7

This is a file CSV standard but some columns need further processing.
Try the following.

str2num <- function(x){
    x <- gsub(",", "", x)
    as.numeric(x)
}

URI <- "https://www.ishares.com/us/products/239600/ishares-msci-acwi-etf/1467271812596.ajax?fileType=csv&fileName=ACWI_holdings&dataType=fund&asOfDate=20141231"
dados <- read.csv(URI, skip = 10, na.strings = c("", "-"))

names(dados)[4] <- "Weight"
cols <- c("Price", "Shares", "Market.Value", "FX.Rate")
dados[cols] <- lapply(dados[cols], str2num)
str(dados)

First we read the file with read.csv.
Then I changed the name of the fourth column (read as "Weight....").
Finally, some columns that appear to be numerical should be class transformed factor for class numeric, with the auxiliary function str2num.

EDITION

In auxiliary function str2num as I originally defined it I used as.character to transform the output of gsub. Now, that is not necessary since gsub already gives us class vectors character. In the section Value page help("gsub") comes (my emphasis)

sub and gsub Return a Character vector of the same length and with the same Attributes as x (after possible coercion to Character).

The original function was as follows.

str2num <- function(x){
    x <- gsub(",", "", x)
    as.numeric(as.character(x))
}
  • good response, very useful

4

The Function read.csv(), already brings from the internet the archive;

 serialize <- read.csv("https://www.ishares.com/us/products/239600/ishares-msci-acwi-etf/1467271812596.ajax?fileType=csv&fileName=ACWI_holdings&dataType=fund&asOfDate=20141231", header = FALSE, sep=",")

For more information on the link below: http://www.sthda.com/english/wiki/reading-data-from-txt-csv-files-r-base-functions

I hope I’ve helped.

  • 4

    It seems to me that the response command leaves the data a little disorganized. I would use serialize <- read.csv("https://www.ishares.com/us/products/239600/ishares-msci-acwi-etf/1467271812596.ajax?fileType=csv&fileName=ACWI_holdings&dataType=fund&asOfDate=20141231", header = TRUE, sep=",", skip=10), so I can skip the first 10 lines and get something more like what I suspect OP wants.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.