mardi 19 mai 2015

Calculating number of unique values with a zero in the adjacent column

Although there may be (what I hope), a simple solution to this, I cannot seem to arrive at one. With a series of paired columns, I want to calculate the number of unique values in every even numbered column (df2, df4, df6) that possesses a zero in its corresponding column on the left (df1,df3,df5).

df1 <- as.data.frame(matrix(sample(0:1000, 36*10, replace=TRUE), ncol=1))
df2 <- as.data.frame(rep(1:12, each=30))
df3 <- as.data.frame(matrix(sample(0:500, 36*10, replace=TRUE), ncol=1))
df4 <- as.data.frame(c(rep(5:12, each=30),rep(1:4, each=30)))
df5 <- as.data.frame(matrix(sample(0:200, 36*10, replace=TRUE), ncol=1))
df6 <- as.data.frame(c(rep(8:12, each=30),rep(1:7, each=30)))
Example <- cbind(df1,df2,df3,df4,df5,df6)
Example <- as.data.frame(Example[which(apply(ntest[-1], 1, function(x) min(x) <= 0)),])

So as an example, if I had the data frame...

df1 df2 df3 df4 df5 df6 
 1   1   9   1   0   1
 0   2   5   2   0   2

... I would want the output to be

df2  df4  df6 
 1    0    2

Thank you in advance.

Aucun commentaire:

Enregistrer un commentaire