【发布时间】:2014-01-26 16:06:21
【问题描述】:
我已经根据一列连续数据的子区间范围拆分了我的数据框:
Data1 <- read.csv(file.choose(), header = T)
# Order (ascending)by size
Group.order <- order(GroupN)
# Assign label to data frame ordered by group
Data1.group.order <- Data1[Group.order, ]
# Set a range of sub-intervals we wish to split the ordered data into
range <- seq(0, 300, by=75)
# Use the split function to split the ordered data, using the cut function which will
# cut the numeric vector GroupN by the value 'range'
Split.Data1 <- split(Data1.group.order, cut(Data1.group.order$GroupN, range))
通过数据拆分,我现在需要在数据框的所有子集中找到其中一列的平均值,但尽管付出了很多努力,我还是很挣扎。
但是,我已经能够使用 lapply 函数在整个拆分数据框中找到多列的平均值,但不能单独找到一列。
任何帮助将不胜感激。
编辑:我是 R 新手,所以我真正想做的是查看数据框每个子集的变量 x 的分布,即 x 轴 = 0-75、75-150、150 -225, 225-300, y 轴 = 变量 x。我的计划是拆分数据,为数据帧的每个子集找到变量 x 的平均值,然后按我对数据帧进行子集划分的间隔绘制变量 x。但是,我确信有更好的方法来做到这一点!
【问题讨论】:
-
或许
lapply(split(DF, f), function(x) mean(x$column_of_interest))之类的内容会有所帮助 -
为什么一开始就滑倒它?也许使用 plyr、dplyr 或 data.table 包更好。
-
如果您需要帮助,您确实需要发布您的数据(或代表性子集),并显示您尝试过的代码。