Im trying to read the following csv file into R
http://asic.gov.au/Reports/YTD/2015/RR20150511-001-SSDailyYTD.csv
The code im currently using is:
url <- "http://asic.gov.au/Reports/YTD/2015/RR20150511-001-SSDailyYTD.csv"
shorthistory <- read.csv(url, skip = 4)
However I keep getting the following error.
1: In readLines(file, skip) : line 1 appears to contain an embedded nul
2: In readLines(file, skip) : line 2 appears to contain an embedded nul
3: In readLines(file, skip) : line 3 appears to contain an embedded nul
4: In readLines(file, skip) : line 4 appears to contain an embedded nul
Which leads me to believe I am utilizing the function incorrectly as it is failing with every line.
Any help would be very much appreciated!
After having lots of issues with CSV files that included a BOM (byte order mark) and NUL, I wrote this little function. It reads the file line-by-line (ignore NUL), skips empty lines, and then applies read.csv
.
# Read CSV files with BOM and NUL problems
read.csvX = function(file, encoding="UTF-16LE", header=T, stringsAsFactors=T) {
csvLines = readLines(file, encoding=encoding, skipNul=T, warn=F)
# Remove BOM (ÿþ) from first line
if (substr(csvLines[[1]], 1, 2) == "ÿþ") {
csvLines[[1]] = substr(csvLines[[1]], 3, nchar(csvLines[[1]]))
}
csvLines = csvLines[csvLines != ""]
if (length(csvLines) == 0) {
warning("Empty file")
return(NULL)
}
csvData = read.csv(text=paste(csvLines, collapse="\n"), header=header, stringsAsFactors=stringsAsFactors)
return(csvData)
}
Hope this answer to an old question helps someone.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With