Home > Blockchain >  How to calculate mean of column, then paste mean value as row value in another data frame in R?
How to calculate mean of column, then paste mean value as row value in another data frame in R?

Time:11-05

I have 36 data frames that each contain columns titled "lon", "lat", and "bottom_temp". Each different data frame represents data from a year between 1980 and 2015. I have a seperate dataframe called "month3_avg_box" that contains two columns: "year" and "avg_bottom_temp". The year column of the "month3_avg_box" data frame contains one row for each year between 1980-2015. I would like to find the average value of each "bottom_temp" column in each of the 36 data frames I have, and place each mean in the corresponding row of the new "month3_avg_box" data frame I have. I will write a mini example of what I'd like:

1980_df:
lon      lat      bottom_temp
-75.61   39.1      11.6
-75.60   39.1      11.5
-75.59   39.1      11.6
-75.58   39.1      11.7

(mean of bottom_temp column for 1980_df = 11.6)

1981_df:
lon      lat      bottom_temp
-75.57   39.1      11.9
-75.56   39.1      11.9
-75.55   39.1      12.0
-75.54   39.1      11.8

(mean of bottom_temp column for 1981_df = 11.9)

1982_df:
lon      lat      bottom_temp
-75.57   39.1      11.6
-75.56   39.1      11.7
-75.55   39.1      11.9
-75.54   39.1      11.2

(mean of bottom_temp column for 1982_df = 11.6)

Now, I'd like to take these averages and put them into my "month3_avg_box" data frame so it looks like:

month3_avg_box:
Year      Avg_bottom_temp
1980        11.6
1981        11.9
1982        11.6

Does this make sense? How can I do this?

CodePudding user response:

We may get the datasets in a list, bind the datasets, create a 'Year' column from the named list, do a group by mean

library(dplyr)
library(stringr)
lst(`1980_df`, `1981_df`, `1982_df`) %>%
    bind_rows(.id = 'Year') %>%
    group_by(Year = str_remove(Year, '_df')) %>%
    summarise(Avg_bottom_temp = mean(bottom_temp))

-output

# A tibble: 3 × 2
  Year  Avg_bottom_temp
  <chr>           <dbl>
1 1980             11.6
2 1981             11.9
3 1982             11.6

data

`1980_df` <- structure(list(lon = c(-75.61, -75.6, -75.59, -75.58), lat = c(39.1, 
39.1, 39.1, 39.1), bottom_temp = c(11.6, 11.5, 11.6, 11.7)), class = "data.frame", row.names = c(NA, 
-4L))
`1981_df` <- structure(list(lon = c(-75.57, -75.56, -75.55, -75.54), lat = c(39.1, 
39.1, 39.1, 39.1), bottom_temp = c(11.9, 11.9, 12, 11.8)), class = "data.frame", row.names = c(NA, 
-4L))
`1982_df` <- structure(list(lon = c(-75.57, -75.56, -75.55, -75.54), lat = c(39.1, 
39.1, 39.1, 39.1), bottom_temp = c(11.6, 11.7, 11.9, 11.2)), class = "data.frame", row.names = c(NA, 
-4L))
  • Related