the*_*ail 31
我想你想?read.csv看看所有选项.
在没有看到您的数据样本的情况下给出确切答案有点困难.
如果您的数据没有标题,并且您知道1985年数据的哪一行开始,您可以使用类似......
impordata <- read.csv(file,skip=1825)
Run Code Online (Sandbox Code Playgroud)
...跳过前1825行.
否则,如果数据中有年份变量,则在导入数据后,您始终可以对数据进行子集化.
impordata <- read.csv("skiplines.csv")
impordata <- subset(impordata,year>=1985)
Run Code Online (Sandbox Code Playgroud)
如果您不知道1985数据的起始位置,可以使用grep在文件的日期变量中查找1985的第一个实例,然后只保留该行的开头:
impordata <- read.csv("skiplines.csv")
impordata <- impordata[min(grep(1985,impordata$date)):nrow(impordata),]
Run Code Online (Sandbox Code Playgroud)
G. *_*eck 20
以下是一些替代方案.(您可能希望之后将第一列转换为"Date"类,并可能将整个事物转换为动物园对象或其他时间序列类对象.)
# create test data
fn <- tempfile()
dd <- seq(as.Date("1980-01-01"), as.Date("1989-12-31"), by = "day")
DF <- data.frame(Date = dd, Value = seq_along(dd))
write.table(DF, file = fn, row.names = FALSE)
Run Code Online (Sandbox Code Playgroud)
read.table + subset
# if file is small enough to fit in memory try this:
DF2 <- read.table(fn, header = TRUE, as.is = TRUE)
DF2 <- subset(DF2, Date >= "1985-01-01")
Run Code Online (Sandbox Code Playgroud)
read.zoo
# or this which produces a zoo object and also automatically converts the
# Date column to Date class. Note that all columns other than the Date column
# should be numeric for it to be representable as a zoo object.
library(zoo)
z <- read.zoo(fn, header = TRUE)
zw <- window(z, start = "1985-01-01")
Run Code Online (Sandbox Code Playgroud)
如果您的数据格式与示例的格式不同,则需要使用其他参数read.zoo.
多个read.table的
# if the data is very large read 1st row (DF.row1) and 1st column (DF.Date)
# and use those to set col.names= and skip=
DF.row1 <- read.table(fn, header = TRUE, nrow = 1)
nc <- ncol(DF.row1)
DF.Date <- read.table(fn, header = TRUE, as.is = TRUE,
colClasses = c(NA, rep("NULL", nc - 1)))
n1985 <- which.max(DF.Date$Date >= "1985-01-01")
DF3 <- read.table(fn, col.names = names(DF.row1), skip = n1985, as.is = TRUE)
Run Code Online (Sandbox Code Playgroud)
sqldf
# this is probably the easiest if data set is large.
library(sqldf)
DF4 <- read.csv.sql(fn, sql = 'select * from file where Date >= "1985-01-01"')
Run Code Online (Sandbox Code Playgroud)