[英]How to summarise and subset multi-level grouped dataframe in dplyr and R
我有以下長格式數據:
testdf <- tibble(
name = c(rep("john", 4), rep("joe", 2)),
rep = c(1, 1, 2, 2, 1, 1),
field = rep(c("pet", "age"), 3),
value = c("dog", "young", "cat", "old", "fish", "young")
)
對於每個命名的人(約翰和喬),我想總結一下他們的每只寵物:
出於某種原因,我似乎無法處理“約翰”數據中的重復事件/寵物。
如果我只為 Joe(只有一只寵物)過濾,則代碼有效。
非常感謝任何幫助......
testdf %>%
group_by(name, rep) %>%
# filter(name == "joe") %>% # when I filter only for Joe, the code works
summarise(
about = paste0(
"The pet is a: ", .[field == "pet", "value"], " and it is ", .[field == "age", "value"]
)
)
testdf %>%
pivot_wider(id_cols = name:rep,names_from = field) %>%
mutate(about = paste0("The pet is a: ", pet, " and it is ", age))
name rep pet age about
<chr> <dbl> <chr> <chr> <chr>
1 john 1 dog young The pet is a: dog and it is young
2 john 2 cat old The pet is a: cat and it is old
3 joe 1 fish young The pet is a: fish and it is young
這也可以用data.table來完成,如下:
library(data.table)
setDT(testdf)[
,j = .(about = paste0("The pet is a ", .SD[field=="pet",value], " and it is ", .SD[field=="age",value])),
by = .(name,rep)
]
name rep about
1: john 1 The pet is a dog and it is young
2: john 2 The pet is a cat and it is old
3: joe 1 The pet is a fish and it is young
您的數據是長格式且不整齊,多個字段合而為一。 所以傳播它或 pivot 更廣泛是 langtang 回答的。 (更好的是 data.table 但我發現仍然很難使用.SD]
我更喜歡在 dplyr 中盡可能簡單地做這些事情。另一種不傳播的方法如下,它會產生相同的結果。 [沒有 data.table where.SD 對我來說仍然很難掌握:所以在 3 行中:
testdf%>%
group_by(name,rep)%>%
summarise(about = paste("The pet is ",value[field=='pet']," and it is ",value[field=='age']))
產量:
name rep about
<chr> <dbl> <chr>
1 joe 1 The pet is fish and it is young
2 john 1 The pet is dog and it is young
3 john 2 The pet is cat and it is old
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.