每日,每月和每年的平均值

时间:2017-10-29 06:06:00

标签: r

我每小时都有数据。我需要转换成每日,每月,然后在年度。

此外,还缺少一些日期,所以我也希望将其包括在内。

#Date
24/02/2000/05:25:00 NaN NaN NaN

26/02/2000/05:10:00 0.227 0.2002496 0.2009378 26/02/2000/06:50:00 NaN NaN NaN 27/02/2000/05:55:00 0.21 0.1687891 0.1630572 28/02/2000/05:00:00 NaN NaN 0.1265696 28/02/2000/06:35:00 0.136 0.1446176 0.1479067 29/02/2000/05:40:00 0.293 0.2279881 0.1900514 01/03/2000/04:45:00 NaN NaN NaN 01/03/2000/06:25:00 0.322 0.3068518 0.2880579 02/03/2000/05:30:00 0.332 0.2793714 0.2391622 02/03/2000/07:05:00 NaN NaN NaN 03/03/2000/06:10:00 0.335 0.2151302 0.2218139 04/03/2000/05:15:00 0.1 0.1138773 0.1168912 04/03/2000/06:55:00 NaN NaN NaN 05/03/2000/06:00:00 0.117 0.1333082 0.147145 06/03/2000/05:05:00 NaN 0.2426362 0.2401871 06/03/2000/06:40:00 NaN 0.32587 0.2845067 07/03/2000/05:45:00 0.323 0.3143821 0.3096662 08/03/2000/04:50:00 NaN NaN NaN 08/03/2000/06:30:00 0.236 0.23232 0.2300107 10/03/2000/06:20:00 0.113 0.1429935 0.1453774 11/03/2000/05:25:00 0.276 0.3238274 0.3150585 11/03/2000/07:00:00 NaN NaN NaN 12/03/2000/06:05:00 0.215 0.2537585 0.2512374 13/03/2000/05:10:00 0.163 0.2273455 0.2679352 13/03/2000/06:50:00 NaN NaN NaN 14/03/2000/05:55:00 0.09 0.1311507 0.1761056 15/03/2000/05:00:00 NaN NaN 0.1447348 15/03/2000/06:35:00 0.125 0.1232291 0.1387782 16/03/2000/05:40:00 0.019 0.06970426 0.11602 17/03/2000/04:45:00 NaN NaN NaN 17/03/2000/06:25:00 0.194 0.1964414 0.1874403 18/03/2000/05:30:00 0.263 0.2749394 0.242199 18/03/2000/07:05:00 NaN NaN NaN 19/03/2000/06:10:00 0.217 0.217737 0.2183706 20/03/2000/05:15:00 0.253 0.2307511 0.2089891 20/03/2000/06:55:00 NaN NaN NaN 21/03/2000/06:00:00 0.282 0.2413632 0.2511235 22/03/2000/05:05:00 NaN 0.382685 0.3944636 22/03/2000/06:45:00 NaN 0.2734097 0.241442 23/03/2000/05:50:00 0.347 0.3289219 0.3003848 24/03/2000/04:50:00 NaN NaN NaN 24/03/2000/06:30:00 0.18 0.1892378 0.2021516 25/03/2000/05:35:00 0.216 0.1871835 0.206762 26/03/2000/06:20:00 0.189 0.1836237 0.2116453 27/03/2000/05:25:00 0.195 0.1817446 0.1804464 27/03/2000/07:00:00 NaN NaN NaN 28/03/2000/06:05:00 0.208 0.168515 0.1819115 29/03/2000/05:10:00 0.162 0.1598227 0.1689523 29/03/2000/06:50:00 NaN NaN NaN 30/03/2000/05:55:00 0.145 0.1472181 0.1723774 31/03/2000/05:00:00 NaN NaN 0.157723 31/03/2000/06:35:00 0.226 0.2108984 0.2339231

2 个答案:

答案 0 :(得分:0)

我猜您在谈论在年,月和日中拆分日期变量,然后您想要计算一些您未在示例中包含的另一个varibale的分组统计信息。如果是这种情况,您可以执行以下操作:

# load package 
library(dplyr)

#Date
 Date <- data.frame( Date =strptime(c("24/02/2000/05:25:00", 
"26/02/2000/05:10:00",
"26/02/2000/06:50:00",
"27/02/2000/05:56:00",
"28/02/2000/05:00:00",
"28/02/2000/06:35:00",
"29/02/2000/05:40:00",
"01/03/2000/04:45:00",
"01/03/2000/06:25:00",
"02/03/2000/05:30:00",
"02/03/2000/07:05:00",
"03/03/2000/06:10:00",
"04/03/2000/05:15:00",
"04/03/2000/06:55:00",
"05/03/2000/06:00:00",
"06/03/2000/05:05:00",
"06/03/2000/06:40:00",
"07/03/2000/05:45:00",
"08/03/2000/04:50:00",
"08/03/2000/06:30:00",
"10/03/2000/06:20:00",
"11/03/2000/05:25:00",
"11/03/2000/07:00:00",
"12/03/2000/06:05:00",
"13/03/2000/05:10:00",
"13/03/2000/06:50:00",
"14/03/2000/05:55:00",
"15/03/2000/05:00:00",
"15/03/2000/06:35:00",
"16/03/2000/05:40:00",
"17/03/2000/04:45:00",
"17/03/2000/06:25:00",
"18/03/2000/05:30:00",
"18/03/2000/07:05:00",
"19/03/2000/06:10:00",
"20/03/2000/05:15:00",
"20/03/2000/06:55:00",
"21/03/2000/06:00:00",
"22/03/2000/05:05:00",
"22/03/2000/06:45:00",
"23/03/2000/05:50:00",
"24/03/2000/04:50:00",
"24/03/2000/06:30:00",
"25/03/2000/05:35:00",
"26/03/2000/06:20:00",
"27/03/2000/05:25:00",
"27/03/2000/07:00:00",
"28/03/2000/06:05:00",
"29/03/2000/05:10:00",
"29/03/2000/06:50:00",
"30/03/2000/05:55:00",
"31/03/2000/05:00:00",
"31/03/2000/06:35:00"), format = "%d/%m/%Y/%H:%M:%S"))


# Split your Date variable in days, months, and years

Date[,"Year"] <- format(Date$Date, format = "%Y")
Date[,"Month"] <- format(Date$Date, format = "%m")
Date[,"Day"] <- format(Date$Date, format = "%d")

# Make up some random variable to calculate summary statistics on

Date[,"Random"] <- sample(seq(1,7,1),size=dim(Date)[1], replace = TRUE)

# Now you can calculate grouped statistics by day, month, or year

MonthMean <- Date %>%
                group_by(Month) %>%
                select(Month, Random) %>%
                summarise(Mean = mean(Random))

# Output 
# A tibble: 2 × 2
  Month     Mean
  <chr>    <dbl>
1    02 3.142857
2    03 4.217391

答案 1 :(得分:0)

我在日,月和年分割了数据然后计算了Daymean,Monthlymean和Annualmean

使用代码:

<configuration>

<configSections>

<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false"/>
<sectionGroup name="devExpress">
  <section name="themes" type="DevExpress.Web.ThemesConfigurationSection, DevExpress.Web.v15.2, Version=15.2.7.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" requirePermission="false"/>
  <section name="compression" type="DevExpress.Web.CompressionConfigurationSection, DevExpress.Web.v15.2, Version=15.2.7.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" requirePermission="false"/>
  <section name="settings" type="DevExpress.Web.SettingsConfigurationSection, DevExpress.Web.v15.2, Version=15.2.7.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" requirePermission="false"/>
  <section name="errors" type="DevExpress.Web.ErrorsConfigurationSection, DevExpress.Web.v15.2, Version=15.2.7.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" requirePermission="false"/>
</sectionGroup>
</configSections>
<appSettings>

<add key="ChartImageHandler" value="storage=memory;deleteAfterServicing=true;" />
</appSettings>
<connectionStrings>

         //here is my database connection only

</connectionStrings>

<system.web>

<compilation debug="true" targetFramework="4.5">


</compilation>
<httpRuntime/>
<pages controlRenderingCompatibilityVersion="4.0">
  <namespaces>
    <add namespace="System.Web.Optimization"/>
  </namespaces>
  <controls>
    <add assembly="Microsoft.AspNet.Web.Optimization.WebForms" namespace="Microsoft.AspNet.Web.Optimization.WebForms" tagPrefix="webopt"/>
    <!--Added new line-->
    <add tagPrefix="asp" namespace="System.Web.UI.DataVisualization.Charting" assembly="System.Web.DataVisualization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
  </controls>
</pages>
<authentication mode="Forms">
  <forms loginUrl="~/Account/Login.aspx" timeout="2880"/>
</authentication>
<profile defaultProvider="DefaultProfileProvider">
  <providers>
    <add name="DefaultProfileProvider" type="System.Web.Providers.DefaultProfileProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" applicationName="/"/>
  </providers>
</profile>
<membership defaultProvider="DefaultMembershipProvider">
  <providers>
    <add name="DefaultMembershipProvider" type="System.Web.Providers.DefaultMembershipProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" applicationName="/"/>
  </providers>
</membership>
<roleManager defaultProvider="DefaultRoleProvider">
  <providers>
    <add name="DefaultRoleProvider" type="System.Web.Providers.DefaultRoleProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" applicationName="/"/>
  </providers>
</roleManager>
<sessionState mode="InProc" timeout = "600" customProvider="DefaultSessionProvider">
  <providers>
    <add name="DefaultSessionProvider" type="System.Web.Providers.DefaultSessionStateProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection"/>
  </providers>
</sessionState>
<httpModules>
  <add type="DevExpress.Web.ASPxHttpHandlerModule, DevExpress.Web.v15.2, Version=15.2.7.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" name="ASPxHttpHandlerModule"/>
</httpModules>
<httpHandlers>
  <add path="DX.ashx" verb="GET" type="DevExpress.Web.ASPxHttpHandlerModule, DevExpress.Web.v15.2, Version=15.2.7.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a"
   validate="false" />
  <add path="ChartImg.axd" verb="GET,HEAD,POST" type="System.Web.UI.DataVisualization.Charting.ChartHttpHandler, System.Web.DataVisualization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
   validate="false" />
</httpHandlers>

</system.web> 
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
  <parameters>
    <parameter value="v13.0"/>
  </parameters>
</defaultConnectionFactory>
</entityFramework>
<system.webServer>

<validation validateIntegratedModeConfiguration="false" />
<handlers>

</handlers>
</system.webServer>

<system.web.extensions>
   <scripting>
       <webServices>
           <jsonSerialization maxJsonLength="50000000"/>
       </webServices>
   </scripting>
 </system.web.extensions>
</configuration>