使用聚合函数以特定方式处理NA

时间:2017-11-24 10:14:53

标签: r dataframe na

我有一个如下所示的数据框:

Project Week Number
Project1   01  46.0
Project2   01  46.4
Project3   01 105.0
Project1   02  70.0
Project2   02  84.0
Project3   02  34.8
Project1   03  83.0
Project3   03  37.9

修改

> dput(my.df)
structure(list(Project = structure(c(1L, 2L, 3L, 1L, 2L, 3L, 
1L, 3L), .Label = c("Project1", "Project2", "Project3"), class = "factor"), 
    Week = c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L), Number = c(46, 
    46.4, 105, 70, 84, 34.8, 83, 37.9)), .Names = c("Project", 
"Week", "Number"), class = "data.frame", row.names = c(NA, -8L
))

我想计算每周每个项目的总和。

所以我使用聚合函数:

aggregate(Number ~ Project + Week, data = my.df, sum)

如您所见,第3周Project2没有任何价值。

使用聚合函数只需将其留空。 我想要的是用0填充该行。

我试过了:

aggregate(Number ~ Project + Week, data = my.df, sum, na.action = 0)

aggregate(Number ~ Project + Week, data = my.df, sum, na.action = function(x) 0)

但没有工作。 有什么想法吗?

3 个答案:

答案 0 :(得分:3)

您可以使用xtabs()

my.df <- read.table(header=TRUE, text=
'Project Week Number
Project1   01  46.0
Project2   01  46.4
Project3   01 105.0
Project1   02  70.0
Project2   02  84.0
Project3   02  34.8
Project1   03  83.0
Project3   03  37.9')
my.df$Week <- paste0("0", my.df$Week)

xtabs(Number ~ Project+Week, data=my.df)
#           Week
# Project       01    02    03
#   Project1  46.0  70.0  83.0
#   Project2  46.4  84.0   0.0
#   Project3 105.0  34.8  37.9
as.data.frame(xtabs(Number ~ Project+Week, data=my.df))
#    Project Week  Freq
# 1 Project1   01  46.0
# 2 Project2   01  46.4
# 3 Project3   01 105.0
# 4 Project1   02  70.0
# 5 Project2   02  84.0
# 6 Project3   02  34.8
# 7 Project1   03  83.0
# 8 Project2   03   0.0
# 9 Project3   03  37.9

答案 1 :(得分:2)

我们还可以使用complete包中的tidyr函数填写Project2Week 3的值。之后,我们可以汇总数据。

library(tidyr)

my.df2 <- my.df %>% 
  complete(Project, Week, fill = list(Number = 0))

my.df2

# # A tibble: 9 x 3
#    Project  Week Number
#      <chr> <chr>  <dbl>
# 1 Project1    01   46.0
# 2 Project1    02   70.0
# 3 Project1    03   83.0
# 4 Project2    01   46.4
# 5 Project2    02   84.0
# 6 Project2    03    0.0
# 7 Project3    01  105.0
# 8 Project3    02   34.8
# 9 Project3    03   37.9

数据

my.df <- read.table(text = "Project Week Number
Project1   '01'  46.0
                 Project2   01  46.4
                 Project3   01 105.0
                 Project1   02  70.0
                 Project2   02  84.0
                 Project3   02  34.8
                 Project1   03  83.0
                 Project3   03  37.9",
                 header = TRUE, stringsAsFactors = FALSE)

my.df$Week <- paste0("0", my.df$Week)

答案 2 :(得分:2)

或者您可以使用spread中的tidyr fill = 0

aggregate(Number ~ Project + Week, data = my.df, sum) %>% 
  spread(key = Week,value = Number,fill = 0)

然后使用聚集将其恢复为原始格式

aggregate(Number ~ Project + Week, data = my.df, sum) %>% 
  spread(key = Week,value = Number,fill = 0) %>% 
  gather(key = Week, value = Number,`1`,`2`,`3`)