gpt4 book ai didi

将非标准 CSV 文件读入 R

转载 作者:行者123 更新时间:2023-12-02 17:02:09 25 4
gpt4 key购买 nike

我正在尝试将以下 csv 文件读入 R

http://asic.gov.au/Reports/YTD/2015/RR20150511-001-SSDailyYTD.csv

我当前使用的代码是:

url <- "http://asic.gov.au/Reports/YTD/2015/RR20150511-001-SSDailyYTD.csv"
shorthistory <- read.csv(url, skip = 4)

但是我不断收到以下错误。

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

这让我相信我错误地使用了该函数,因为它在每一行都失败了。

任何帮助将不胜感激!

最佳答案

由于左上角的空白,read.csv() 似乎不起作用。必须逐行读取文件 (readLines()),然后跳过前 4 行。

下面显示了一个示例。文件作为文件连接 (file()) 打开,然后逐行读取 (readLines())。前 4 行通过子集化被跳过。该文件以制表符分隔,以便递归应用 strsplit()。它们仍然保留为字符串列表,并且应将它们重新格式化为数据帧或任何其他合适的类型。

# open file connection and read lines
path <- "http://asic.gov.au/Reports/YTD/2015/RR20150511-001-SSDailyYTD.csv"
con <- file(path, open = "rt", raw = TRUE)
text <- readLines(con, skipNul = TRUE)
close(con)

# skip first 4 lines
text <- text[5:length(text)]
# recursively split string
text <- do.call(c, lapply(text, strsplit, split = "\t"))

text[[1]][1:4]
# [1] "1-PAGE LTD ORDINARY" "1PG " "1330487" "1.72"

关于将非标准 CSV 文件读入 R,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30251576/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com