【问题标题】:R: How to read a csv file containing non-dataset informationR:如何读取包含非数据集信息的 csv 文件
【发布时间】:2015-11-19 14:58:54
【问题描述】:

我有一个在记事本中不显示换行符的 .csv 文件。 Notepad++ 最后显示了 LF 字符,但我不知道如何告诉 R 使用该字符作为换行符,或者如何用 CRLF 或 \n 替换它。

**编辑:here 是一个示例文件。

【问题讨论】:

  • 最终fread()从包data.table可以读取文件。
  • 或者你转换文件(通常也是一个好的编辑程序可以做到)cyberciti.biz/faq/…
  • 在 Windows 上使用read.csv 读取 CR、LF、CRLF 或 LFCR 没有问题。
  • 也许提供您用来尝试读取文件的代码....
  • 代码为:file

标签: r csv notepad++


【解决方案1】:

使用我们快速、友好的文件整理器:

library(data.table)

url <- 'https://dl.dropboxusercontent.com/u/8428744/Collaboration_vs_Publication_Year.csv'

# ignore first 14 rows per OP comment
df <-fread(url, skip = 14) # in this case, it works even without skip=

# put first 14 rows somewhere else
other_stuff <- readLines(url, n=14)

警告信息:在 fread("https://dl.dropboxusercontent.com/u/8428744/Collaboration_vs_Publication_Year.csv") :在空行 23 处停止阅读,但之后存在文本 (废弃):“© 2015 Elsevier B.V. 版权所有。SciVal ® 是 Reed Elsevier Properties S.A. 的注册商标,用于 许可证。”

df
#                            V1 V2   V3   V4   V5   V6   V7   V8   V9  V10
# 1:           Brown University NA 0.80 0.84 0.81 0.79 0.79 0.79 0.76 0.64
# 2:        Columbia University NA 0.98 0.96 0.95 0.96 1.00 1.01 0.97 1.26
# 3:         Cornell University NA 0.94 0.92 0.93 0.95 0.93 0.98 0.94 1.26
# 4:          Dartmouth College NA 0.74 0.79 0.70 0.75 0.74 0.75 0.73 0.60
# 5:         Harvard University NA 1.08 1.05 1.06 1.10 1.09 1.10 1.08 0.97
# 6:       Princeton University NA 1.04 0.99 1.02 1.06 1.08 1.05 1.06 0.87
# 7: University of Pennsylvania NA 0.80 0.78 0.79 0.83 0.81 0.80 0.79 0.83
# 8:            Yale University NA 0.93 0.90 0.92 0.95 0.91 0.97 0.90 1.07

cat(other_stuff[nchar(other_stuff)>0], sep = '\n')
# Data set,Collaboration vs Publication Year
# Entities,"Brown University, Columbia University, Cornell University, Dartmouth College, Harvard University, Princeton University, University of Pennsylvania, Yale University"
# Year range,2010 to >2015
# Filtered by,"not filtered"
# Data source,Scopus
# Date last updated,16 October 2015
# Date exported,19 November 2015
# Metric name,Specific metric,Self-citations,Types of publications included,Other options
# Collaboration,International collaboration,-,"Articles, reviews and conference papers","field-weighted"
# Name,Tags,Collaboration,
# ,,Overall,2010,2011,2012,2013,2014,2015,>2015,

【讨论】:

  • 谢谢!这很好,但我如何保留前 14 行?最终我想将前 14 行和数据集解析为两个不同的文件。
  • 谢谢,这真的很有帮助!如果您不介意解释,那么 fread 的作用与 read.csv 有何不同?似乎 read.csv 猜测基于第一行有 2 列,我什至无法开始猜测为什么 fread 会跳过所有这些行并巧妙地返回 8X10 数据集。
  • 这对@MattDowle 来说是一个很好的问题,但我认为你的猜测很好。我只能指向?fread: Once the separator is found on line autostart, the number of columns is determined. Then the file is searched backwards from autostart until a row is found that doesn't have that number of columns. Thus, the first data row is found and any human readable banners are automatically skipped. .... 我敢打赌read.csv 从第 1 行开始,而不是这个灵活的自动启动。我正在编辑您的标题以反映问题可能是什么(即,不是 LF)。
【解决方案2】:

正如您提到的,您希望保留所有数据,您可以尝试以下操作。源文件很乱,所以这不是一个完全自动化的解决方案,未来的文件需要额外的按摩。

myfile <- readLines("https://dl.dropboxusercontent.com/u/8428744/Collaboration_vs_Publication_Year.csv")

df1 <- read.csv(text=myfile, skip = grep("Overall", myfile) - 1)
df2 <- read.csv(text=myfile, nrows = grep("Overall", myfile) - 1, header = FALSE)
finaldf <- data.frame(df1[, colSums(is.na(df1)) != nrow(df1)], t(unstack(df2, V2 ~ V1)))[-nrow(df1), ]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-01-25
    • 1970-01-01
    • 2021-01-09
    • 2011-04-19
    • 1970-01-01
    • 2012-03-26
    • 1970-01-01
    相关资源
    最近更新 更多