thatsciencegal
thatsciencegal

Reputation: 113

Filtering individual dates in R, dplyr

I have a large dataset with about 60 years of data, and I'm trying to make daily calculations to the water level column in the dataset which contains daily water levels at different locations. To do this, I think I need to filter out each date individually, which is very time-consuming. Is there a faster way to do this? I thought about writing a function, but even with this, I would still have to type in the individual dates (I think?).

Here is the head and tail of my data:

X Date Water.Level..cm. Lat Long 1 1 1977-11-01 NA -12.80861 -48.24028 2 2 1977-11-02 NA -12.80861 -48.24028 3 3 1977-11-03 NA -12.80861 -48.24028 4 4 1977-11-04 NA -12.80861 -48.24028 5 5 1977-11-05 NA -12.80861 -48.24028 6 6 1977-11-06 NA -12.80861 -48.24028 350719 350719 1938-01-26 635 -5.533333 -47.48333 350720 350720 1938-01-27 667 -5.533333 -47.48333 350721 350721 1938-01-28 710 -5.533333 -47.48333 350722 350722 1938-01-29 717 -5.533333 -47.48333 350723 350723 1938-01-30 721 -5.533333 -47.48333 350724 350724 1938-01-31 690 -5.533333 -47.48333

And this is what I currently have: nov1 <- dat %>% filter(Date == as.Date("1989-11-01"))

I would like to end up with different data frames containing all of the data in master data frame for each date:

X Date Water.Level..cm. Lat Long 1 4384 1989-11-01 711 -12.80861 -48.24028 2 10684 1989-11-01 NA -12.47056 -48.21139 3 10929 1989-11-01 240 -12.44722 -48.26806 4 15373 1989-11-01 275 -12.35500 -48.25889 5 37414 1989-11-01 246 -12.30444 -48.26222 6 51326 1989-11-01 240 -12.26194 -48.32917

Upvotes: 1

Views: 154

Answers (2)

jalind
jalind

Reputation: 481

If you want different data frames containing all of the information in the master data frame for each individual date, you could use the split() function.

df_list <- split(dat, as.factor(dat$Date))

Upvotes: 2

Rich Pauloo
Rich Pauloo

Reputation: 8412

Use group_by() and summarise()

You're looking for a way to summarise your data by a factor, in this case Date.

data %>% group_by(Date) %>% summarise(mean = mean(Water.Level..cm.))

This returns a table of grouped summaries. See this link for more information.

Upvotes: 1

Related Questions