Download CSV file using R

2

Good afternoon everyone!

How do I download (read) a CSV file from a link like this:

link

Thank you!

    
asked by anonymous 24.01.2018 / 13:12

2 answers

7

This is a CSV standard file 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)

We first read the file with read.csv . Then I changed the name of the fourth column (it is read as "Weight...." ). Finally, some columns that appear to be numeric must be transformed from class factor to class numeric , with the helper function str2num .

EDITION

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

  

sub and gsub return to 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))
}
    
24.01.2018 / 19:07
4

The read.csv () function, already brings the file from the internet;

 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: link

I hope I have helped.

    
24.01.2018 / 13:37