csarvf_01
csarvf_01

Reputation: 61

Creating new column in R based on different column with a for loop

I try to create a new column in R, which contains the mean of values of a different column but for their respective date.

My data frame looks something like this:

Temp  Date
4    2018-01-01
3    2018-01-01
2    2018-01-02
2    2018-01-02

I now want to create a third column, with the mean temperature for each day. So that it looks like this:

Temp  Date       mean_Temp
4    2018-01-01   3.5
3    2018-01-01   3.5
2    2018-01-02    2
2    2018-01-02    2

I already tried:

 for (i in as.list(df$Date)) {
   df$mean_Temp[i] <- paste(mean(df$Temp))
}

But that doesn't work, it only returns the overall mean of the temperature and doesn't calculate the mean for every day individually. Thank you guys, I hope I made my problem clear.

Upvotes: 0

Views: 40

Answers (2)

Javier Herrero
Javier Herrero

Reputation: 121

Try:

library(dplyr)
df %>% group_by(Date) %>% mutate(mean_Temp = mean(Temp))

When grouping with dplyr, you can either use summarise or mutate. summarise will return one row per group while mutate will add/modify one column and repeat the value for all the entries in each group.

Upvotes: 0

benson23
benson23

Reputation: 19142

I would not use a for loop in this case, since it is utterly unnecessary.

Here is a tidyverse approach. Based on your desired output, each Date would still have two records after the mean is calculated. If you only want a single row for each Date, use summarise() instead of mutate().

mutate()

library(tidyverse)

df %>% group_by(Date) %>% mutate(mean_Temp = mean(Temp))

# A tibble: 4 x 3
# Groups:   Date [2]
   Temp Date       mean_Temp
  <dbl> <chr>          <dbl>
1     4 2018-01-01       3.5
2     3 2018-01-01       3.5
3     2 2018-01-02       2  
4     2 2018-01-02       2  

summarise()

df %>% group_by(Date) %>% summarize(mean_Temp = mean(Temp))

# A tibble: 2 x 2
  Date       mean_Temp
  <chr>          <dbl>
1 2018-01-01       3.5
2 2018-01-02       2  

Upvotes: 2

Related Questions