汇总向量,然后将汇总统计信息附加到R中的原始数据帧

汇总向量,然后将汇总统计信息附加到R中的原始数据帧,r,dplyr,reshape2,summary,mutate,R,Dplyr,Reshape2,Summary,Mutate,简介: ## Creating our dataframe: datetime <- c("5/12/2017 16:15:00","5/16/2017 16:45:00","5/19/2017 17:00:00") datetime <- as.POSIXct(datetime, format = "%m/%d/%Y %H:%M:%S") values <- c(1,2,3) df <- data.frame(datetime, values) ## Here's t

简介:

## Creating our dataframe:
datetime <- c("5/12/2017 16:15:00","5/16/2017 16:45:00","5/19/2017 17:00:00")
datetime <- as.POSIXct(datetime, format = "%m/%d/%Y %H:%M:%S")
values <- c(1,2,3)
df <- data.frame(datetime, values)

## Here's the current output:
head(df)
             datetime values
1 2017-05-12 16:15:00      1
2 2017-05-16 16:45:00      2
3 2017-05-19 17:00:00      3

## And here's the desired output:
head(df1)
             datetime values mean    sd    se
1 2017-05-12 16:15:00      1    2 0.816 0.471
2 2017-05-16 16:45:00      2    2 0.816 0.471
3 2017-05-19 17:00:00      3    2 0.816 0.471
我想计算给定数据帧中数值向量的平均值、标准偏差和标准误差,然后使用这些汇总统计信息创建三个新向量。然后我需要将它们与原始数据帧结合起来

示例代码:

## Creating our dataframe:
datetime <- c("5/12/2017 16:15:00","5/16/2017 16:45:00","5/19/2017 17:00:00")
datetime <- as.POSIXct(datetime, format = "%m/%d/%Y %H:%M:%S")
values <- c(1,2,3)
df <- data.frame(datetime, values)

## Here's the current output:
head(df)
             datetime values
1 2017-05-12 16:15:00      1
2 2017-05-16 16:45:00      2
3 2017-05-19 17:00:00      3

## And here's the desired output:
head(df1)
             datetime values mean    sd    se
1 2017-05-12 16:15:00      1    2 0.816 0.471
2 2017-05-16 16:45:00      2    2 0.816 0.471
3 2017-05-19 17:00:00      3    2 0.816 0.471
##创建我们的数据帧:

datetime您可以同时完成作业。假设您已经有了helper函数供您选择
sd
se

sd0 <- function(x){sd(x) / sqrt(length(x)) * sqrt(length(x) - 1)}
se0 <- function(x){ sd0(x) / sqrt(length(x))}

sd0这是
dplyr
解决方案,在
mt1022
的回答中给出了
sd0
se0

df %>% mutate("mean"=mean(values),"sd"=sd0(values),"se"=se0(values))

创建新的列并按如下方式分配平均值:
df$mean哇,太简单了,谢谢!我应该更清楚。。。我一直在想如何用dplyr实现这一点……或者:
sd0nice!我肯定需要更熟悉
lappy()
@spacedSparking,出于好奇,为什么在R中使用有偏差的估计而不是默认的
sd
。这是个好问题。。。我在
df$sd之间得到的数字略有不同,因为
sd
使用
sqrt(n-1)
作为分母。仅供参考,请参阅sd和的手册。这太棒了。谢谢分享!