R:如何读取包含非数据集信息的csv文件

时间:2015-11-19 14:58:54

标签: r csv notepad++

我有一个.csv文件,它不会在记事本中显示换行符。 Notepad ++最后显示了LF字符,但我无法弄清楚如何告诉R将该字符用作换行符或如何用CRLF或\ n替换它。

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

2 个答案:

答案 0 :(得分:3)

使用我们快速,友好的文件finagler:

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,

答案 1 :(得分:0)

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

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), ]