在R中匹配和汇总数据框

时间:2015-03-21 18:52:39

标签: r indexing group-summaries

我有一个包含时间序列数据列的数据框,并开始年末年。

df = data.frame(y2000=c(12,636),y2001=c(234, 76),y2002=c(3434, 46),y2003=c(36,35),y2004=c(6, 64),   y2005=c(56,65), y2006=c(43,65), y2007=c( 6, 56),y2008=c( 64, 66),y2009=c(63, 5656),y2010 = c(65,54),startyear= c(2006, 2001), endyear= c(2009, 2005))

对于每一行,我想计算开始和结束年份内以及开始和结束时段之前和之后的平均值。所需的输出如下:

y2000   y2001   y2002   y2003   y2004   y2005   y2006   y2007   y2008   y2009   y2010   startyear   endyear before_mean within_mean  after_mean
12  234 3434    36  6   56  43  6   64  63  65  2006    2009    629.6666667     44  65
636 76  46  35  64  65  65  56  66  5656    54  2001    2005    636 57.2        1179.4

我尝试过不同的匹配和索引技术,但不能围绕这一点。

3 个答案:

答案 0 :(得分:3)

<强> 1。 dplyr / tidyr

最好转换广泛的&#39;格式为&#39; long&#39;格式。我们可以使用dplyr/tidyr来获取mean。创建一个&#39; ind&#39;列,将数据重新整理为“长”字样。使用gather,拆分&#39;变量&#39;将列分为两列(&#39; var1&#39;,&#39; var2&#39;)与extract,分组&#39; ind&#39;,获取mean值价值&#39;基于创建的不同逻辑索引对其进行子集化的列(即var2 < startyearvar2 >= startyear & var2 <= endyearvar2 >endyear

library(dplyr)
library(tidyr)

dS <-  df %>%
          mutate(ind=row_number()) %>%
          gather(variable, value, starts_with('y')) %>%
          extract(variable, c('var1', 'var2'), '([^0-9]+)([0-9]+)',
                        convert=TRUE) %>%
          group_by(ind) %>%
          summarise(before_mean= mean(value[var2 < startyear]), 
                   within_mean = mean(value[var2 >= startyear & 
                                            var2 <= endyear]),
                   after_mean=mean(value[var2 >endyear])) %>% 
         as.data.frame()

nm1 <-  paste(c('before', 'within', 'after'), 'mean', sep="_")
dS
#   ind before_mean within_mean after_mean
#1   1    629.6667        44.0       65.0
#2   2    636.0000        57.2     1179.4

我们可以在&#39; df&#39;中创建其他列。从上面的输出

df[nm1] <- dS

<强> 2。基础R

我们可以使用base R方法,而无需更改数据集的格式。从原始数据集(&#39; df&#39;)创建数字列名称的索引(&#39; indx&#39;),删除非数字部分并转换为数字(&#39; v1&# 39)。

 indx <- grep('\\d+', names(df))
 v1 <- as.numeric(sub('[^0-9]+', '', names(df)[indx]))

循环播放&#39; df&#39; (lapply),match&#39; startyear&#39;使用&#39; v1&#39;,使用该索引(&#39; i1&#39;)获取列unlist,并计算mean。同样可以通过匹配&#39; endyear&#39;用&#39; v1&#39;得到索引(&#39; i2&#39;)。基于&#39; i1&#39;和&#39; i2&#39;,计算&#39; within_mean&#39;和&#39; after_mean&#39;。 rbind列表元素并将输出分配到&#39; df&#39;中的新列(&#39; nm1&#39;)。

df[nm1] <- do.call(rbind,lapply(1:nrow(df), function(i) {
       i1 <- match(df$startyear[i], v1)
       before_mean<-  mean(unlist(df[i,1:(i1-1),drop=FALSE]))
       i2 <- match(df$endyear[i], v1)
       within_mean <- mean(unlist(df[i,i2:i1]))
      after_mean <- mean(unlist(df[i,match(v1[(i2+1):length(v1)],v1)]))
       data.frame(before_mean,within_mean, after_mean) }))
 df[nm1]
 #    before_mean within_mean after_mean
 #1    629.6667        44.0       65.0
 #2    636.0000        57.2     1179.4

答案 1 :(得分:3)

另一种方法比akrun还要使用Base R.我们将以与列名相同的顺序创建一个中间变量,但具有数字格式。这将用于引用实际数据帧的列:

col.years <- suppressWarnings(as.numeric(sub("^y", "", colnames(df))))[1:11]

# Initialise everything to NA (better when preparing to loop over df)        
df$before_mean <- NA
df$within_mean <- NA
df$after_mean <- NA

for(i in seq_len(nrow(df))) {
    df$before_mean[i] <- mean(as.numeric(df[i, which(col.years < df$startyear[i])]))
    df$within_mean[i] <- mean(as.numeric(df[i, which((col.years >= df$startyear[i]) & (col.years <= df$endyear[i]))]))
    df$after_mean[i]  <- mean(as.numeric(df[i, which(col.years > df$endyear[i])]))
}

<强>结果

df[,14:16]

#   before_mean within_mean after_mean
# 1    629.6667        44.0       65.0
# 2    636.0000        57.2     1179.4

答案 2 :(得分:1)

这是一个解决方案:

#The original data:
df = data.frame(y2000=c(12,636),y2001=c(234, 76),y2002=c(3434, 46),y2003=c(36,35),y2004=c(6, 64),   y2005=c(56,65), y2006=c(43,65), y2007=c( 6, 56),y2008=c( 64, 66),y2009=c(63, 5656),y2010 = c(65,54),startyear= c(2006, 2001), endyear= c(2009, 2005))

df$s = df$startyear - 1999
df$e = df$endyear - 1999
df$before_mean <- apply(df, 1, function(x)sum(x[1:(x[14]-1)] ))
df$within_mean <- apply(df, 1, function(x)sum(x[x[14]:x[15]] ))
df$after_mean <- apply(df, 1, function(x)sum(x[(x[15]+1):11] ))
df$s <- NULL
df$e <- NULL

这个解决方案与示例中的确切年份相关联,但要使其更通用也不会太难。