Conditionally sum dynamic columns in r
问题 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