Read csv from specific row

前端 未结 3 1412
后悔当初
后悔当初 2020-12-04 18:27

I have daily data starting from 1980 in csv file. But I want to read data only from 1985. Because the other dataset in another file starts from 1985. How can I skip reading

3条回答
  •  既然无缘
    2020-12-04 19:02

    I think you want to take a look at ?read.csv to see all the options.

    It's a bit hard to give an exact answer without seeing a sample of your data.

    If your data doesn't have a header and you know which line the 1985 data starts on, you can just use something like...

    impordata <- read.csv(file,skip=1825)
    

    ...to skip the first 1825 lines.

    Otherwise you can always just subset the data after you've imported it if you have a year variable in your data.

    impordata <- read.csv("skiplines.csv")
    impordata <- subset(impordata,year>=1985)
    

    If you don't know where the 1985 data starts, you can use grep to find the first instance of 1985 in your file's date variable and then only keep from that line onwards:

    impordata <- read.csv("skiplines.csv")
    impordata <- impordata[min(grep(1985,impordata$date)):nrow(impordata),]
    

提交回复
热议问题