I am trying to conditionally sum across many columns depending on if they are greater than or less than 0. I am surprised I cannot find a dplyr
or data.table
work around for this. I want to calculate 4 new columns for a large data.frame (columns to calculate are at bottom of post).
dat2=matrix(nrow=10,rnorm(100));colnames(dat2)=paste0('V',rep(1:10))
dat2 %>% as.data.frame() %>%
rowwise() %>%
select_if(function(col){mean(col)>0}) %>%
mutate(sum_pos=rowSums(.)) ##Obviously doesn't work
These are the simple statistics I want to calculate (yes; these apply statements work, but there are other things in my dplyr chain I want to do, so thats why I am looking for a dplyr
or data.table
way. The columns that are positive or negative for each given row are different, so I cannot grab a list of columns (must be done dynamically, by row).
#Calculate these, but in a dplyr chain?
n_pos=apply(dat2,1,function(x) sum((x>0)))
n_neg=apply(dat2,1,function(x) sum((x<0)))
sum_pos=apply(dat2,1,function(x) sum(x[(x>0)]))
sum_neg=apply(dat2,1,function(x) sum(x[(x<0)]))