-2

I got the following question. Currently I am working with RStudio and i want to get a daily mean, however I just started to learn Rstudio and don't know how to continue...

my data set looks like this:

  1. row (day): 2019-12-01; 2019-12-01; 2019-12-02; 2019-12-02; 2019-12-02; 2019-12-03;.....
  2. row (value): 1; 2; 3; 1; 3; 3; 1;...

And i want my dataset to look like this:

  1. row (day): 2019-12-01; 2019-12-02; 2019-12-03;.....
  2. row (average_value): 1; 2; 3;....

Which Code do I need to get it like that? Can someone help me? Thank you very much!!

Mick
  • 1

2 Answers2

1

you can also use library dplyr

library(dplyr)
value<-c(3,7,3,4,5)
DF<-data.frame(Day=c("2019-12-01","2019-12-01","2019-12-02","2019-12-02","2019-12-02"),value=value)

Day value
1 2019-12-01     3
2 2019-12-01     7
3 2019-12-02     3
4 2019-12-02     4
5 2019-12-02     5

DF %>% group_by(Day) %>% summarize(average = mean(value)) 

Day        average
  <fct>        <dbl>
1 2019-12-01       5
2 2019-12-02       4

dplyr makes this very easy through the use of the group_by() function, which splits the data into groups. When the data is grouped in this way summarize() can be used to collapse each group into a single-row summary. summarize() does this by applying an aggregating or summary function to each group, built-in functions are mean, median, min, and max

Sebin Sunny
  • 1,753
  • 10
  • 9
0

You can use aggregate and mean to get daily mean like:

aggregate(value ~ day, yourDataset, mean)
#         day    value
#1 2019-12-01 1.500000
#2 2019-12-02 2.333333
#3 2019-12-03 2.000000

Data:

yourDataset <- structure(list(day = structure(c(18231, 18231, 18232, 18232, 
18232, 18233, 18233), class = "Date"), value = c(1, 2, 3, 1, 
3, 3, 1)), class = "data.frame", row.names = c(NA, -7L))
GKi
  • 37,245
  • 2
  • 26
  • 48