Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
310 views
in Technique[技术] by (71.8m points)

How can I group categorical variable that also meets another criteria in R? DPLYR?

I have a task that I can't solve. My goal is to be able to figure out how many "families" have children (under 18). I only need the sum of unique familyids and I've tried doing it in R and Excel and can't figure it out.

In my data I have four families and my data is saved on a client level.

data <- data.frame(
  
  "FamilyID" = c(10,10,10,11,11,11,12,12,13,13),
  "ClientID" = c(101,102,103,111,112,113,121,122,131,132),
  "Age" = c(26,1,5,35,34,1,54,60,17,21)
)

My goal is to have something like this

Metric                             Count
Families w/ Children                3
Families w/out Children             1

In my actual dataset I have thousands of families so I really appreciate ant help. How can I do this with dplyr?

question from:https://stackoverflow.com/questions/65945197/how-can-i-group-categorical-variable-that-also-meets-another-criteria-in-r-dply

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
library(tidyverse)
counts <- data %>%
  group_by(FamilyID) %>%
  summarise(number_of_children = sum(Age<= 18), number_of_adults = sum(Age > 18)) %>%
  ungroup()

final <- counts %>%
  summarise("Families w/ children" = sum(number_of_children > 0), "Families w/o children" = sum(number_of_children < 1)) %>%
  gather() %>%
  rename("Metric" = key, "Count" = value)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...