Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在R中按日期平均多个变量?_R_Dataframe_Time Series_Average - Fatal编程技术网

如何在R中按日期平均多个变量?

如何在R中按日期平均多个变量?,r,dataframe,time-series,average,R,Dataframe,Time Series,Average,我需要找到一种方法获得每日平均温度和压力,并保留id和城市名称。我给出了示例数据,但我的数据更复杂,需要处理更多的列(降水等)和行(更多的城市和时间) 示例数据: id city temperature pressure time 1 1 new_york 15 1000 2015-01-01 06:30:00 2 1 new_york 16 1003 2015-01-01 18

我需要找到一种方法获得每日平均温度和压力,并保留id和城市名称。我给出了示例数据,但我的数据更复杂,需要处理更多的列(降水等)和行(更多的城市和时间)

示例数据:

 id      city temperature pressure                time
    1  1  new_york          15     1000 2015-01-01 06:30:00
    2  1  new_york          16     1003 2015-01-01 18:30:00
    3  3    london          13      980 2015-01-01 07:00:00
    4  3    london          12      998 2015-01-01 20:30:00
    5  5 barcelona          30     1013 2015-01-01 08:00:00
    6  5 barcelona          32     1015 2015-01-01 12:00:00
data <- data.frame("id" = c(1,1, 3,3,5,5),
                   "city" = c("new_york", "new_york", "london", "london", "barcelona", "barcelona"),
                   "temperature" = c(15, 16, 13, 12, 30, 32),
                   "pressure" =  c(1000, 1003, 980, 998, 1013, 1015),
                   "time" = c("2015-01-01 06:30:00","2015-01-01 18:30:00",
                              "2015-02-10 07:00:00", "2015-02-10 20:30:00",
                              "2015-04-08 08:00:00", "2015-04-08 12:00:00"),stringsAsFactors = FALSE)
我想得到:

  id      city temperature pressure       time
1  1  new_york        15.5   1001.5 2015-01-01
2  3    london        12.5    989.0 2015-02-10
3  5 barcelona        31.0   1014.0 2015-04-08
生成示例数据的代码:

 id      city temperature pressure                time
    1  1  new_york          15     1000 2015-01-01 06:30:00
    2  1  new_york          16     1003 2015-01-01 18:30:00
    3  3    london          13      980 2015-01-01 07:00:00
    4  3    london          12      998 2015-01-01 20:30:00
    5  5 barcelona          30     1013 2015-01-01 08:00:00
    6  5 barcelona          32     1015 2015-01-01 12:00:00
data <- data.frame("id" = c(1,1, 3,3,5,5),
                   "city" = c("new_york", "new_york", "london", "london", "barcelona", "barcelona"),
                   "temperature" = c(15, 16, 13, 12, 30, 32),
                   "pressure" =  c(1000, 1003, 980, 998, 1013, 1015),
                   "time" = c("2015-01-01 06:30:00","2015-01-01 18:30:00",
                              "2015-02-10 07:00:00", "2015-02-10 20:30:00",
                              "2015-04-08 08:00:00", "2015-04-08 12:00:00"),stringsAsFactors = FALSE)
data试试这个

library(dplyr)
data %>% group_by(id, city, time = as.Date(time)) %>% summarise(across(c(temperature, pressure), mean))
输出

# A tibble: 3 x 5
# Groups:   id, city [3]
     id city      time       temperature pressure
  <dbl> <chr>     <date>           <dbl>    <dbl>
1     1 new_york  2015-01-01        15.5    1002.
2     3 london    2015-02-10        12.5     989 
3     5 barcelona 2015-04-08        31      1014 
#一个tible:3 x 5
#组:id,城市[3]
城市时间温度压力
纽约大学2015-01-01 15.5 1002。
伦敦2015-02-1012.5989
巴塞罗那2015-04-08 31 1014

从“时间”列中删除时间,然后按平均值分组。聚合数据是R中最常见的问题之一。请在提问之前先进行操作。