简体   繁体   English

如何计算列的平均值,然后将平均值粘贴为 R 中另一个数据框中的行值?

[英]How to calculate mean of column, then paste mean value as row value in another data frame in R?

I have 36 data frames that each contain columns titled "lon", "lat", and "bottom_temp".我有 36 个数据框,每个数据框都包含标题为“lon”、“lat”和“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".每个不同的数据框代表 1980 年到 2015 年之间一年的数据。我有一个名为“month3_avg_box”的单独数据框,它包含两列:“year”和“avg_bottom_temp”。 The year column of the "month3_avg_box" data frame contains one row for each year between 1980-2015. “month3_avg_box”数据框的年份列包含 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.我想在我拥有的 36 个数据帧中的每一个中找到每个“bottom_temp”列的平均值,并将每个平均值放在我拥有的新“month3_avg_box”数据帧的相应行中。 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) (1980_df 的 bottom_temp 列的平均值 = 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) (1981_df 的 bottom_temp 列的平均值 = 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) (1982_df 的 bottom_temp 列的平均值 = 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”数据框中,如下所示:

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?我怎样才能做到这一点?

We may get the datasets in a list我们可能会在list获取数据集

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))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM