0
votes

Say I have a data frame consisting of a number of rows, like this:

df <- data.frame(Group = c(0,0,1,1,1,0),V1=c(0,0,0,4,5,7), V2=c(0,3,0,4,0,1))

  Group V1 V2
1     0  0  0
2     0  0  3
3     1  0  0
4     1  4  4
5     1  5  0
6     0  7  1

Group is binary, V1 and V2 have zero-inflation (many observations == 0) I'd like to subset each column (in turn) to remove 0 obs and then calculate quantiles on remaining data. Crucially, I'd like to remove 0s for a given variable only, and not remove whole rows, as I'd be wanting to reset and subset again for the next column.

I have my code for quantiles below. Is there any way I can sneak in the subset function or do I need an different approach?

#Functions for quantiles
quant25 <- function(x) quantile(x, probs=0.25, na.rm=TRUE)
quant50 <- function(x) quantile(x, probs=0.50, na.rm=TRUE)  
quant75 <- function(x) quantile(x, probs=0.75, na.rm=TRUE)

#Grouped calls on these functions
group_by(df, Group) %>%
summarise_each(funs(quant25, quant50, quant75), V1, V2)
1
If that gives you the desired result, you can add that as an answer and accept that.Jaap
What about 'filter_at(vars(V1,V2), any_cars(. != 0))' before 'group_by()'MikolajM

1 Answers

0
votes

I think I've figured this one out for my purposes: df[,2:3][df[,2:3]==0] <- NA to declare 0 observations missing and the rest seems to handle as expected. (Thanks, Jaap)