r - How to generate counts of positive values by column in data.table -


i have data tables consist of several columns , many thousands of rows. data looks like:

iteration v1  v2  v3  v4 1         -2  3   -4   1 2         -2  3   -3   4 3         -2  3    7   -8 4         -2  3   -4   2 5         -2  3   -4   -3 

i have been trying figure out how calculate counts of positive values in each column, , proportion of positive counts counts in column.

this seems simple can't figure out how output data.table has counts column in it.

i can combining bunch of following statements, there has better way- advice tired mind?

nrow(dat[v2>=0]) 

assuming dataframe called df:

df <- data.frame('v1'=c(-2, -2, -2, -2, -2), 'v2'=c(3, 3, 3, 3, 3), 'v3'=c(-4, -3, 7, -4, -4), 'v4'=c(1, 4, -8, 2, -3)) 

you start defining number of rows as:

nrows <- dim(df)[1] 

then, can define auxiliary function such:

calcstats <- function(x) {   pos <- sum(df[, x] > 0)   c("number of positives" = pos, "proportion of positives" = pos / nrows)   } 

and result with:

result <- as.data.frame(map(calcstats, colnames(df)))                          v1 v2  v3  v4 number of positives      0  5 1.0 3.0 proportion of positives  0  1 0.2 0.6 

Comments

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

reactjs - React router and this.props.children - how to pass state to this.props.children -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -