R 将函数应用于拆分数据帧的所有子集上的一列

R 将函数应用于拆分数据帧的所有子集上的一列,r,function,split,apply,R,Function,Split,Apply,我已根据一列连续数据的一系列子间隔分割数据帧: 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-inte

我已根据一列连续数据的一系列子间隔分割数据帧:

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))

Data1用
plyr做这样的事情怎么样

require(plyr) # library

dat<-data.frame(x=sample(1:300,300),y=runif(300)*10)   # create random data
head(dat)

#    x        y
#1 193 2.580328
#2 119 4.519489
#3  51 5.340437
#4 114 9.249253
#5 236 4.756849
#6 108 5.926478

ddply(dat,                                                 # use dat
      .(grp=cut(dat$x,seq(0,300,75),seq(0,300,75)[-1])),   # group by formula (cut)
      summarise,                                           # tell ddply to summarise
      mean=mean(y),                                        # calc mean
      sum=sum(y))                                          # calc sum

#  grp     mean      sum
#1  75 4.620653 346.5490
#2 150 5.337813 400.3360
#3 225 4.238518 317.8889
#4 300 4.996709 374.7532
require(plyr)#库

DAT也许类似于
lappy(分割(DF,f),函数(x)平均值(x$column\u of\u interest))
这样的东西是有帮助的,为什么要把它放在第一位呢?也许使用plyr、dplyr或data.table包是更好的选择。如果您需要帮助,您确实需要发布数据(或代表性子集),并显示您尝试过的代码。