簡體   English   中英

R-帶條件的轉置列和行

[英]R - Transpose columns and rows with conditions

我正在使用數據框“ by_class_survival”,正在嘗試以其他格式進行轉換,更改行和列以及包括條件,我已經以一種非常粗糙的方式解決了問題,所以我想知道是否有更好的方法轉置列和行,同時添加條件以創建轉置。

library(dplyr)

titanic_tbl <- dplyr::tbl_df(Titanic)
titanic_tbl <- titanic_tbl %>%
mutate_at(vars(Class:Survived), funs(factor))
by_class_survival <- titanic_tbl %>%
group_by(Class, Survived) %>%
summarize(Count = sum(n))

原始數據框

# Class Survived Count
# 1 1st   No         122
# 2 1st   Yes        203
# 3 2nd   No         167
# 4 2nd   Yes        118
# 5 3rd   No         528
# 6 3rd   Yes        178
# 7 Crew  No         673
# 8 Crew  Yes        212

根據by_class_survival中的值創建一個新的數據框

first <- c(122,203)
second <- c(167, 118)
third <- c(528,178)
crew <- c(673,212)

titanic.df = data.frame(first,second,third,crew)

library(data.table)
t_titanic.df <- transpose(titanic.df)
rownames(t_titanic.df) <- colnames(titanic.df)
colnames(t_titanic.df) <- c("No survivor", "Survivor")

預期結果

##        No survivor Survivor
## first          122      203
## second         167      118
## third          528      178
## crew           673      212

有沒有更好的方法來達到預期的結果?

您可以使用reshape2::dcast一步reshape2::dcast

library(reshape2)
library(dplyr)

titanic_tbl %>%
 dcast(Class ~ Survived, value.var = "n", sum)

  Class  No Yes
1   1st 122 203
2   2nd 167 118
3   3rd 528 178
4  Crew 673 212

或者您可以在匯總數據框上使用tidyr::spread

library(tidyr)
titanic_tbl %>%
  group_by(Class, Survived) %>% 
  summarise(sum = sum(n)) %>%
  spread(Survived, sum)

# A tibble: 4 x 3
# Groups:   Class [4]
  Class    No   Yes
  <chr> <dbl> <dbl>
1 1st     122   203
2 2nd     167   118
3 3rd     528   178
4 Crew    673   212

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM