Reading specific lines from a csv file in R

I have a large csv file that I need to read into R. However, I only need observations with specific variable values ​​(i.e., specific dates). Is there a way to do this from the start without having to read the whole file and then a subset?

+3


source to share


1 answer


Assuming the dates are in the first column of your dataset (and you are on a Unix-like computer), you can do something like this:

dates <- paste0(c("2015-06-01", "2015-06-16"), collapse = "|")
expr <- paste0("grep -E '(", dates, "),.+' tmpcsv.csv", collapse = "")
##
R> data.table::fread(expr)
           V1         V2
1: 2015-06-16 -1.6866933
2: 2015-06-16  1.3686023
3: 2015-06-01 -0.2257710
4: 2015-06-16 -1.0185754
5: 2015-06-01  0.3035286
6: 2015-06-01  2.0500847
7: 2015-06-01 -0.4910312

      

If not, you will have to change the regex accordingly.




Data:

set.seed(123)
##
df <- data.frame(
  Date = Sys.Date() + floor(50*round(runif(50, -1, 1), 1)),
  Value = rnorm(50)
)
write.csv(df, file = "tmpcsv.csv", row.names = FALSE)
##

      

+2


source







All Articles