dcast总结单列

时间:2017-03-22 23:06:59

标签: r data.table dplyr reshape2

我想调整我的数据,以便使用dcast获得平均存活率,但似乎可能无法实现:

数据

PassengerId Survived    Pclass  Name    Sex Age SibSp   Parch   Ticket  Fare    Cabin   Embarked
1   0   3   Braund, Mr. Owen Harris male    22  1   0   A/5 21171   7.25        S
2   1   1   Cumings, Mrs. John Bradley (Florence Briggs Thayer) female  38  1   0   PC 17599    71.2833 C85 C
3   1   3   Heikkinen, Miss. Laina  female  26  0   0   STON/O2. 3101282    7.925       S

示例数据代码:

df <- structure(list(PassengerId = 1:6, Survived = structure(c(1L, 
                                                                  2L, 2L, 2L, 1L, 1L), .Label = c("0", "1"), class = "factor"), 
                        Pclass = c(3L, 1L, 3L, 1L, 3L, 3L), Name = c("Braund, Mr. Owen Harris", 
                                                                     "Cumings, Mrs. John Bradley (Florence Briggs Thayer)", "Heikkinen, Miss. Laina", 
                                                                     "Futrelle, Mrs. Jacques Heath (Lily May Peel)", "Allen, Mr. William Henry", 
                                                                     "Moran, Mr. James"), Sex = c("male", "female", "female", 
                                                                                                  "female", "male", "male"), Age = c(22, 38, 26, 35, 35, NA
                                                                                                  ), SibSp = c(1L, 1L, 0L, 1L, 0L, 0L), Parch = c(0L, 0L, 0L, 
                                                                                                                                                  0L, 0L, 0L), Ticket = c("A/5 21171", "PC 17599", "STON/O2. 3101282", 
                                                                                                                                                                          "113803", "373450", "330877"), Fare = c(7.25, 71.2833, 7.925, 
                                                                                                                                                                                                                  53.1, 8.05, 8.4583), Cabin = c("", "C85", "", "C123", "", 
                                                                                                                                                                                                                                                 ""), Embarked = c("S", "C", "S", "S", "S", "Q")), .Names = c("PassengerId", 
                                                                                                                                                                                                                                                                                                              "Survived", "Pclass", "Name", "Sex", "Age", "SibSp", "Parch", 
                                                                                                                                                                                                                                                                                                              "Ticket", "Fare", "Cabin", "Embarked"), row.names = c(NA, 6L), class = "data.frame")

到目前为止的功能:

reshape2::dcast(titanic, Sex ~ ., mean)

期望的输出:

Row Label  Average of Survived 
Male       3.14156  
Female     3.14156

目前,它会返回此错误:

     Sex  .
1 female NA
2   male NA
Warning messages:
1: In mean.default(.value[0], ...) :
  argument is not numeric or logical: returning NA

我认为这可能与重塑中的强制转换函数有关,但这可能与reshape2有关吗?

3 个答案:

答案 0 :(得分:2)

如何使用dplyr进行尝试?

library(dplyr)
output <-  df  %>% 
  dplyr::mutate(Survived = as.numeric(as.character(Survived))) %>%  
  dplyr::select(Sex, Survived) %>% 
  dplyr::group_by(Sex) %>% 
  dplyr::summarise(average_of_survived = mean(Survived))
output
## A tibble: 2 × 2
#     Sex average_of_survived
#   <chr>               <dbl>
#1 female                   1
#2   male                   0

答案 1 :(得分:2)

所以,你确实可以使用dcast,但Survived是一个因素,它抛出一个错误,你需要定义哪个列你想用作计算值。结果列顺序无关紧要,这是令人惊讶的。

df$Survived <- as.numeric(as.character(df$Survived))
reshape2::dcast(df, Sex~., mean, value.var = "Survived")
#     Sex .
#1 female 1
#2   male 0

答案 2 :(得分:2)

可以使用dcast()(或reshape2)软件包中的data.table来完成此操作,如OP's own answer所示。

如果没有dcast(),您也可以直接使用data.table进行汇总:

library(data.table)
setDT(df)[, Survived := as.numeric(as.character(Survived))][, mean(Survived), by = Sex]
#      Sex V1
#1:   male  0
#2: female  1
{p> df用于Q中的dput()。链接用于形成&#34;单行&#34;。

以上更简洁的版本将是

setDT(df)[, mean(as.numeric(as.character(Survived))), by = Sex]