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
1.0k views
in Technique[技术] by (71.8m points)

datatable - Is there an R function to keep all rows even if criteria not met?

I have a data table of teams data by number of players by Region and want to copy to an existing spreadsheet that logs by region by team within certain size category (1, 2, 3-7, etc) in some cases teams are not within the criteria (0) so they do not pull into the data table. How do I ensure the data table continues to show these rows with a 0 rather than removing all together?

Data is available here: https://github.com/rweingarten/Team-Data

team_sizes <- setDT(teams)[, .(sum = sum(uniqueN(player_id))), by = list(REGION, team_ID)]
team_size_3<-subset(team_sizes, team_sizes$sum >= 3 & team_sizes$sum <= 7, by = (team_sizes$REGION))
team_size_three <- setDT(team_size_3)[, uniqueN(team_ID), by = REGION]

This team_size_three results in:

team_size_three
   PA  3
   GB  1
   NE  1
   NJ  5
   NY  1
   OK  2

but it should display:

team_size_three
   AR  0
   PA  3
   GA  1
   NE  1
   NJ  5
   NY  1
   OK  2
   MD  0

where you can see AR and MD pulling in 0s.


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

1 Reply

0 votes
by (71.8m points)
等待大神答复

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

...