通过随后的因子进行变异

在这个问题底部的data.frame中,我想按年份和年龄计算个人数量,创建年份减年龄的年份变量,然后创建年龄和年份的因子版本,类变量。 使用group_by() ,从base RI的dplyrfactor() summarize()mutate() ,得到以下结果:

d <- group_by(d,year,age) %>% summarize(catch=n())
d1 <- mutate(d,yrclass=year-age,fage=as.factor(age),fyrclass=as.factor(yrclass))
d1

Source: local data frame [27 x 6]
Groups: year

   year age catch yrclass fage fyrclass
1  2010   3    13    2007    3     2007
2  2010   4    10    2006    4     2006
3  2010   5    19    2005    5     2005
4  2010   6    24    2004    6     2004
5  2010   7    19    2003    7     2003
6  2010   8    32    2002    8     2002
7  2010   9    22    2001    9     2001
8  2010  10    13    2000   10     2000
9  2011   2     5    2009    3       NA
10 2011   3     9    2008    4       NA
..  ... ...   ...     ...  ...      ...

人们可以在第9行和第10行看到“age”和“yrclass”的分解版本是不正确的。

但是,如果我从我的data.frame中去除非data.frame类(即删除dplyr相关的类),那么这些错误消失。

d <- as.data.frame(d)
d2 <- mutate(d,yrclass=year-age,fage=as.factor(age),fyrclass=as.factor(yrclass))
d2  # only first 10 rows shown

   year age catch yrclass fage fyrclass
1  2010   3    13    2007    3     2007
2  2010   4    10    2006    4     2006
3  2010   5    19    2005    5     2005
4  2010   6    24    2004    6     2004
5  2010   7    19    2003    7     2003
6  2010   8    32    2002    8     2002
7  2010   9    22    2001    9     2001
8  2010  10    13    2000   10     2000
9  2011   2     5    2009    2     2009
10 2011   3     9    2008    3     2008

这是一个错误还是我错过了什么?

数据低于(来自dput)。 我试图做一个更小的重现性示例,但我无法确定导致相同错误的模式。

d <- structure(list(year = c(2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 
2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 
2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 
2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 
2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 
2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 
2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 
2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 
2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 
2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 
2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 
2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 
2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 
2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 
2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 
2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 
2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 
2010L, 2010L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 
2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 
2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 
2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 
2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 
2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 
2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 
2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 
2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 
2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 
2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 
2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 
2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 
2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 
2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 
2011L, 2011L, 2011L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 
2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 
2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 
2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 
2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 
2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 
2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 
2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 
2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 
2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 
2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 
2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 
2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 
2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 
2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 
2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 
2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L), age = c(3L, 
3L,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
9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 10L, 10L, 10L, 
10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 11L, 11L, 11L, 11L, 11L, 
11L, 11L, 11L, 11L, 11L, 11L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 
8L, 8L, 8L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 
10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 12L, 12L)),
.Names = c("year","age"), class = "data.frame", row.names = c(NA, -436L))

如果添加缺少的结尾%>% ungroup() ;它会正常工作。 您正在跳过拆分应用组合中的组合步骤。 我不知道这是甚至定义的行为,试图对grouped_df而不是数据grouped_df进行操作。

d <- group_by(d,year,age) %>% summarize(catch=n()) %>% ungroup()

mutate(d,yrclass=year-age,fage=as.factor(age),fyrclass=as.factor(yrclass))
Source: local data frame [27 x 6]

   year age catch yrclass fage fyrclass
1  2010   3     1    2007    3     2007
2  2010   4     1    2006    4     2006
3  2010   5     1    2005    5     2005
4  2010   6     1    2004    6     2004
5  2010   7     1    2003    7     2003
6  2010   8     1    2002    8     2002
7  2010   9     1    2001    9     2001
8  2010  10     1    2000   10     2000
9  2011   2     1    2009    2     2009
10 2011   3     1    2008    3     2008
..  ... ...   ...     ...  ...      ...

顺便说一下, 你可以压缩你的测试用例 (使用rle )来获得:

d <- data.frame(year = c(rep(2010,152), rep(2011,136),rep(2012,148)),
     age =  c(rep(3,13), rep(4,10), rep(5,19), rep(6,24), rep(7,19),
            rep(8,32), rep(9,22),  rep(10,13), rep(2,5), rep(3,9),
            rep(4,11), rep(5,13), rep(6,19), rep(7,12),  rep(8,17),
            rep(9,28), rep(10,11), rep(11,11), rep(3,26), rep(4,47),
            rep(5,23), rep(6,13), rep(7,11), rep(8,7), rep(9,9),
            rep(10,10), rep(12,2)) )

>which we got from
> rr <- rle(d$age)
> print(paste('rep(', unlist(as.list(paste(rr$values, rr$lengths, sep=','))) , '),', sep='' ), quote=F)
链接地址: http://www.djcxy.com/p/24787.html

上一篇: by followed by factor in mutate

下一篇: Dynamically select data frame columns using $ and a vector of column names