Reputation: 25
I am relatively new to R I have a large data set which gives several data values for that day. So to simplify matters I need to get an average value for each day in in one table displaying the day and the average value.
Date_Recorded Value
2016-08-19 74.2
2016-08-19 74.6
2016-08-20 85.63
2016-08-20 88.55
And I would like the resulting table to look like this
Date_Recorded Value
2016-08-19 74.4
2016-08-20 87.09
Then after this How would I go about pulling out a date range of data say from 2016-08-20 to 2018-02-04 out of this data set or any other dataset?
Upvotes: 0
Views: 590
Reputation: 5138
Great answer by Chabo. Alternatively, you could use the tidyverse approach:
library(tidyverse)
Date_Recorded<-c("2016-08-19", "2016-08-19", "2016-08-20",
"2016-08-20", "2016-08-21", "2016-08-21")
Value <- c(74.2, 74.6, 85.63,
88.55, 70.1, 70.2)
df<-data.frame(Date_Recorded,Value)
df$Date_Recorded<-as.Date(df$Date_Recorded)
# To create the resulting table you wanted
df %>%
group_by(Date_Recorded) %>%
summarise(mean(Value, na.rm = FALSE))
# Or to search for a date range. You could use filter(Date_Recorded == "2018-10-02") to
# serach for a single date
df %>%
filter(Date_Recorded >= "2016-08-20" & Date_Recorded <= "2016-08-21") %>% #to select a date range
group_by(Date_Recorded) %>%
summarise(mean(Value, na.rm = FALSE))
Upvotes: 1
Reputation: 3000
We can do this using aggregate, part of the base statistics in R...
Date_Recorded<-c(
"2016-08-19",
"2016-08-19",
"2016-08-20",
"2016-08-20")
Value<-c(
74.2,
74.6,
85.63,
88.55
)
df<-data.frame(Date_Recorded,Value)
df$Date_Recorded<-as.Date(df$Date_Recorded)
test_df<-aggregate(df["Value"], by=df["Date_Recorded"], FUN=mean)
> test_df
Date_Recorded Value
1 2016-08-19 74.40
2 2016-08-20 87.09
# As pointed out by @Sotos
start_date<-as.Date("2016-08-18")
end_date<-as.Date("2016-08-19")
test_df[test_df$Date_Recorded >= start_date & test_df$Date_Recorded <=
end_date, ]
Date_Recorded Value
1 2016-08-19 74.4
Credit to @Sotos for the second half of this question.
Upvotes: 0