firmo23
firmo23

Reputation: 8464

Count or sum the values of 2 columns based on the value of another column in the same dataframe

I have the dataframe below:

year<-c("2000","2000","2001","2002","2000","2002")
gender<-c("M","F","M","F","M","M")
weight<-c(0.5,0.7,0.8,0.7,0.6,0.9)
YG<-data.frame(year,gender,weight)

and I want to count the gender for years 2000 and 2001 and sum the weight for year 2002 in order to create a new dataframe like:

year   M   F
1 2000 2.0 1.0
2 2001 1.0 0.0
3 2002 0.9 0.7

I tried something like:

library(tidyverse)
YG %>%
  group_by(year) %>%
  summarise(sum(weight[year=="2002"]))%>%
  count(round(gender[year!="2002"])) %>%
  spread(gender, n, fill = 0)

Upvotes: 2

Views: 417

Answers (4)

Patrik_P
Patrik_P

Reputation: 3200

Given your data you could use dcast:

library(data.table)
setDT(YG)
result <- dcast(YG,  year ~ gender, value.var = 'weight', fun = list(sum, length))
result[, .(year, 
           `F` = c(result$weight_length_F[1:2], result$weight_sum_F[3]),
           M = c(result$weight_length_M[1:2], result$weight_sum_M[3]))]

#year   F   M
#1: 2000 1.0 2.0
#2: 2001 0.0 1.0
#3: 2002 0.7 0.9

Alternativelly, you could call dcast two times on the subseted datasets by year like follows:

result2 <- rbindlist(list(
      dcast(YG[year != 2002], year ~ gender, value.var = 'weight', fun = length),
      dcast(YG[year == 2002], year ~ gender, value.var = 'weight', fun = sum)))

#   year   F   M
#1: 2000 1.0 2.0
#2: 2001 0.0 1.0
#3: 2002 0.7 0.9

Upvotes: 0

Rui Barradas
Rui Barradas

Reputation: 76683

I believe that now I've got it right.

library(tidyverse)

YG %>%
  group_by(year, gender) %>%
  summarise(n = sum(weight),
            g = n()) %>%
  mutate(n = ifelse(year == 2002, n, g)) %>%
  select(-g) %>%
  spread(gender, n, fill = 0)
## A tibble: 3 x 3
## Groups:   year [3]
#  year      F     M
#  <fct> <dbl> <dbl>
#1 2000    1     2  
#2 2001    0     1  
#3 2002    0.7   0.9

Upvotes: 2

DaveArmstrong
DaveArmstrong

Reputation: 22074

One possibility is to pre-process how you want to deal with the "weight". Essentially, you want to add the weights for 2002, but add weights of 1 for other years. You could first do this:

YG <- YG %>% add_column(wt = ifelse(year == 2002, weight, 1))

Then, you could aggregate and use the dcast function in the data.table package to rearrange the results.

YG %>% group_by(year, gender) %>% summarise(count = sum(wt)) %>% 
   dcast(formula = year~gender,fun.aggregate = sum,value.var = "count")

  year   F   M
1 2000 1.0 2.0
2 2001 0.0 1.0
3 2002 0.7 0.9

Upvotes: 1

jon
jon

Reputation: 368

Because of the logic you're using, I think using case_when() would work pretty well. If you set the logic before summarizing then all you need to do is sum the two columns:

library(tidyverse)
library(data.table)
YG %>% 
  mutate(Male = case_when(gender == 'F' ~ 0,
                          year %in% c('2000', '2001') & gender == 'M'~1,
                          TRUE~weight),
         Female = case_when(gender == 'M' ~ 0,
                            year %in% c('2000', '2001') & gender == 'F'~1,
                            TRUE~weight)) %>%
  group_by(year) %>%
  summarize(M = sum(Male),
            F = sum(Female))

This will give you what you're looking for:

  year   M   F
1 2000 2.0 1.0
2 2001 1.0 0.0
3 2002 0.9 0.7

Upvotes: 2

Related Questions