计算行的方式 [英] Calculate means of rows
问题描述
我有一个名为 ants
的数据框,详细说明每个站点的多个条目,如下所示:
I have a dataframe called ants
detailing multiple entries per site, looks like this:
Site Date Time Temp SpCond Salinity Depth Turbidity Chlorophyll
1 71 6/8/2010 14:50:35 14.32 49.88 32.66 0.397 0.0 1.3
2 71 6/8/2010 14:51:00 14.31 49.94 32.70 1.073 0.0 2.0
3 71 6/8/2010 14:51:16 14.32 49.95 32.71 1.034 -0.1 1.6
4 71 6/8/2010 14:51:29 14.31 49.96 32.71 1.030 -0.2 1.6
5 70 6/8/2010 14:53:55 14.30 50.04 32.77 1.002 -0.2 1.2
6 70 6/8/2010 14:54:09 14.30 50.03 32.77 0.993 -0.5 1.2
网站具有不同数量的条目,通常为3,但有时较少或更多。如果日期和站点号码匹配,我想编写一个新的数据框,每个站点有一个条目,详细说明每个参数的平均值/平均读数。我想要从计算和后续数据框中省略空或na单元格。
Sites have different numbers of entries, usually 3 but sometimes less or more. Where both date and site number match I would like to write a new dataframe with one entry per site detailing the average/mean readings for each parameter. I would like empty or "na" cells to be omitted from the calculation and subsequent dataframe.
我不知道这是一个应用功能还是一个版本的行也许?非常困难,任何帮助非常感谢!
I'm not sure if this is an apply function or a version of rowMeans maybe? Very stuck, any help much appreciated!
推荐答案
这是一个完整的新答案,全面的日志也涵盖了您的新规范: / p>
Here is a complete new answer with a full log also covering your new specification:
R> Lines <- " Site Date Time Temp SpCond Salinity Depth Turbidity Chlorophyll
+ 71 6/8/2010 14:50:35 14.32 49.88 32.66 0.397 0.0 1.3
+ 71 6/8/2010 14:51:00 14.31 49.94 32.70 1.073 0.0 2.0
+ 71 6/8/2010 14:51:16 14.32 49.95 32.71 1.034 -0.1 1.6
+ 71 6/8/2010 14:51:29 14.31 49.96 32.71 1.030 -0.2 1.6
+ 70 6/8/2010 14:53:55 14.30 50.04 32.77 1.002 -0.2 1.2
+ 70 6/8/2010 14:54:09 14.30 50.03 32.77 0.993 -0.5 1.2
+ "
R> con <- textConnection(Lines)
R> df <- read.table(con, sep="", header=TRUE, stringsAsFactors=FALSE)
R> close(con)
R> df$pt <- as.POSIXct(strptime(paste(df$Date, df$Time), "%m/%d/%Y %H:%M:%S"))
R> library(plyr)
R> newdf <- ddply(df, .(Site,Date), function(x) mean(x[,-(1:3)], na.rm=TRUE))
R> newdf$pt <- as.POSIXct(newdf$pt, origin="1970-01-01")
R> newdf
Site Date Temp SpCond Salinity Depth Turbidity Chlorophyll pt
1 70 6/8/2010 14.30 50.03 32.77 0.9975 -0.350 1.200 2010-06-08 20:54:02
2 71 6/8/2010 14.32 49.93 32.70 0.8835 -0.075 1.625 2010-06-08 20:51:05
R>
这篇关于计算行的方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!