Skip to content

Instantly share code, notes, and snippets.

@padpadpadpad
Last active January 11, 2017 09:52
Show Gist options
  • Save padpadpadpad/b276a2f5a1abc8d09094feca57b027db to your computer and use it in GitHub Desktop.
Save padpadpadpad/b276a2f5a1abc8d09094feca57b027db to your computer and use it in GitHub Desktop.
library(dplyr)
library(tidyr)
# create dummy data
Class <- rep(seq(1,3,1),2)
Count <- runif(length(Class),50,100)
Quad_ID <- c(rep("A",3),rep("B",3))
x <- data.frame(Class,Count,Quad_ID)
# change class 2 and class 1 to "new"
# group dataframe by Quad_ID and Class
# sum counts
# create proportion of counts column for each Quad_id
# keep just "new" data
y <- mutate(x, Class = ifelse(Class %in% c(1,2), 'new', Class)) %>%
group_by(., Quad_ID, Class) %>%
summarise(., Count = sum(Count)) %>%
ungroup(.) %>%
group_by(., Quad_ID) %>%
mutate(., prop = Count/sum(Count)) %>%
filter(., Class == "new") %>%
data.frame()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment