0

Ok, so lets take some simple data

A <- c(1997,2000,2000,1998,2000,1997,1997,1997)
B <- c(0,0,1,0,0,1,0,0)
df <- data.frame(A,B)

Now if we run the following

length(df$B[df$A==1997& df$B==0])
length(df$B[df$A==1997& df$B==1])

We get the counts of each unique A and B combinations.

But I want to output a results that will search for each unique A value (representing a year) and output the sum of B (being binary). The results can be a data.frame or matrix. Any suggestions on how to tackle such a problem?

The results could look like: enter image description here

Frank
  • 66,179
  • 8
  • 96
  • 180
lukeg
  • 1,327
  • 3
  • 10
  • 27
  • 2
    Just `table(df)`, man. Or `with(df, table(B, A))` or exact output. Or `library(reshape2) ; dcast(df, B ~ A, length, value.var = "B")`. – David Arenburg Jul 27 '15 at 14:34
  • Following what @DavidArenburg suggested you can simply do `t(table(df))` which will give you the requested table. – Konrad Jul 27 '15 at 14:45
  • @Konrad don't think transposing a whole data set just in order to save keystrokes is a good practice. – David Arenburg Jul 27 '15 at 15:04

0 Answers0