Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/81.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.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_Aggregate - Fatal编程技术网

在r中使用分类变量进行聚合

在r中使用分类变量进行聚合,r,aggregate,R,Aggregate,我需要按日期汇总我的“日”数据集: > 但包括与房间号对应的第6列: newDay <- aggregate(Day[, 6:9], list(Day$Date), mean,na.rm=TRUE) newDay既然您不需要TimeDay,我就删除它,因为无法应用均值函数。我将使用dplyr的“每个人的总结”和“分组”,而不是“汇总”。在你的例子中,你使用了mean,所以我也使用了它 Day$TimeDay <- NULL library(dplyr) ne

我需要按日期汇总我的“日”数据集:

>

但包括与房间号对应的第6列:

newDay <- aggregate(Day[, 6:9], list(Day$Date), mean,na.rm=TRUE)

newDay既然您不需要TimeDay,我就删除它,因为无法应用均值函数。我将使用dplyr的“每个人的总结”和“分组”,而不是“汇总”。在你的例子中,你使用了mean,所以我也使用了它

    Day$TimeDay <- NULL
    library(dplyr)
    newDay <- summarise_each(group_by(Day, Date), funs(mean)) %>%
              select(-Day, -Month, -Year, -Room)

Day$TimeDay您的意思是要按日期和房间号进行聚合吗?按日期和房间号如果“房间”列是一个因素,它不应该是您的分组变量之一,而不是结果变量之一吗?您希望这样:
aggregate(as.matrix(Day[,7:9])~Date+room),FUN=mean,na.rm=TRUE,data=Day)
@AnandaMahto我需要的是一个新的数据框,包含日期、房间、温度、光线、相对湿度,按日期聚合自从您引入dplyr以来,使用
..%>%删除列会更容易吗选择(-Day,-Month,-Year)
?谢谢@mmstan!也许,我没有清楚地解释我想做什么。我想得到一个按日期聚合的数据帧。从数据集中可以看出,2013年9月2日有许多观测值,我想计算该日期的平均温度,以便每天只获得一个观测值/行。当我重新创建数据集时,我确实得到了您想要的输出,每行一个日期和温度平均值。是否必须使用聚合函数?@mmstan您尝试过吗?newDay%select(-Day,-Month,-Year)为什么“房间”列的结果为NA?@Luisa function mean()应用于“房间”列,如果它生成NA,“房间”列中可能有NAs。但是,即使没有NAs,使用您编写的命令,也会得到房间号的平均值,这不是非常有用的信息。您可能希望在“group_by(Day,Date,Room)”中包含Room,以便按日期和Room聚合行,或者如果Room不相关,则使用此部分“select(-Day,-Month,-Year,-Room)”将其从结果数据框中删除。
    Day$TimeDay <- NULL
    library(dplyr)
    newDay <- summarise_each(group_by(Day, Date), funs(mean)) %>%
              select(-Day, -Month, -Year, -Room)