Reputation: 487
I have a dataset which looks like the one below:
| Id | Name | Date_diff |
|----|:-----:|----------:|
| 50 | David | 0 |
| 50 | David | -16 |
| 50 | David | -4 |
| 50 | David | -1 |
| 50 | David | 0 |
| 50 | David | -2 |
| 84 | Ron | -11 |
| 84 | Ron | -12 |
| 84 | Ron | -168 |
| 84 | Ron | -8 |
| 84 | Ron | 16 |
| 84 | Ron | NA |
Reproducible code is:
df= data.frame(Id= c('50','84'), Name= c('David','Ron'))
df=df[rep(seq_len(nrow(df)),each=6),]
Date_diff= c(0,-16,-4,-1,0,-2,-11,-12,-168,-8,16,'NA')
df=data.frame(df,Date_diff)
Now, for each Id, I need to create different columns of unequal buckets which will have the count of values in the column 'Date-diff'. The bucket ranges need to be 'NA', '>0','0','-1','-2 to -3', '-4 to -6','-7 to -12' and '>-12'. There will also be an additional column 'total' which will hold the summed values present in the buckets.
For example, when we consider Id=50, we see that there are 2 counts for the value '0' which would fall in the bucket '0', 1 count for the value '-16' which would fall in the bucket '>0', 1 count for the value -4 which would fall in the range '-4 to -6' and so on. The final table should be as represented below:
| Id | Name | NA | >0 | 0 | -1 | -2 to -3 | -4 to -6 | -7 to -12 | >-12 | Total |
|----|:-----:|---:|----|---|----|----------|----------|-----------|------|-------|
| 50 | David | 0 | 0 | 2 | 1 | 1 | 1 | 0 | 1 | 6 |
| 84 | Ron | 1 | 1 | 0 | 0 | 0 | 0 | 3 | 1 | 6 |
I initially tried to create a new column to categorize the values in 'Date_diff' in it, but the values being provided in breaks is probably wrong. This is what I have tried:
df <- transform(df, group=cut(Date_diff, breaks=c(-Inf,-13,-7,-4,-2,-1,Inf),
labels=c('<-12', '-7 to -12','-4 to -6','-2 to -3', '-1','>0')))
Can someone please let me know how to achieve the desired result?
Upvotes: 1
Views: 51
Reputation: 476
One of the issues was having 'NA'
as a character string instead of NA
. Here is a solution with:
df <- data.frame(
id = c('50', '84'),
name = c('david', 'ron'),
date_diff = c(0, -16, -4, -1, 0, -2, -11, -12, -168, -8, 16, na)
)
library(dplyr)
library(tidyr)
df %>%
mutate(
group = cut(
Date_diff,
breaks = c(-Inf,-13,-7,-4,-2,-1,Inf),
labels = c('<-12', '-7 to -12','-4 to -6','-2 to -3', '-1','>0')
),
group = if_else(is.na(group), "NA", as.character(group))
) %>%
group_by(Id, Name, group) %>%
summarise(n = n()) %>%
mutate(Total = sum(n, na.rm = T)) %>%
pivot_wider(names_from = group, values_from = n)
Upvotes: 1
Reputation: 107652
Original cut
assignment could work with help of within
to re-assign special groups, table
to count values by cut groups, and reshape
to convert long to wide format
cut
+ within
# CREATE CUT COLUMN
df <- within(df, {
Group <- as.character(cut(Date_diff,
breaks=c(-Inf,-13,-7,-4,-2,-1,Inf),
labels=c('<-12','-7 to -12','-4 to -6',
'-2 to -3','-1','>0')))
# ADJUST FOR ZERO AND ONE GROUPING
Group <- ifelse(Date_diff == 0, "0", ifelse(Date_diff == 1, "1", Group))
})
# TABULATE COUNTS
tbl_df <- transform(data.frame(table(Name=df$Name, Group = df$Group, useNA = "ifany"),
stringsAsFactors = FALSE),
Group = ifelse(is.na(Group), "NA", as.character(Group)))
# RESHAPE
final_df <- reshape(tbl_df, v.names = "Freq", timevar = "Group", idvar = "Name",
direction = "wide", sep = "_")
# REORDER AND RENAME COLUMNS
cols <- c("NA", ">0", "0", "-1", "-2 to -3", "-4 to -6", "-7 to -12", "<-12")
final_df <- setNames(final_df[c("Name", paste0("Freq_", cols))],
c("Name", gsub("Freq_", "", cols)))
# ADD TOTAL AND ID COLUMNS
final_df$Total <- rowSums(final_df[-1])
final_df <- merge(unique(df[c("Id", "Name")]), final_df, by="Name")[c("Id", "Name", cols)]
Output
final_df
# Name Id NA >0 0 -1 -2 to -3 -4 to -6 -7 to -12 <-12 Total
# 1 David 50 0 0 2 1 1 1 0 1 6
# 2 Ron 84 1 1 0 0 0 0 3 1 6
Upvotes: 0