Search code examples
rcountaggregatesummary

Count values in column use empty cells to indicate new number


I want to use behavioural data to calculate the number of items caught. This is my example data:

df <- data.frame(id = as.factor(c(51,51,51,51,51,51,51,52,52,52,52,52,52)), 
             type = c("(K)","(K)","(K)","(K)","","","","(K)","(K)","","(K)","","(K)"))

I would like to count each of my "K"'s based on if they are consecutive or not. If consecutive, the string should count as one. if there is a gap between, they should both count as one.. so final tally will be 2.

Hope that makes sense... for the example above, I would like my final output data to look like this

id type tally
1 51  (K)     1
2 52  (K)     3

I thought aggregate might do this, however it counts the total number in a column so for 51 tally=4 rather than 1

Any help would be appreciated

Thanks Grace


Solution

  • The rle command in base R would be useful.

    temp<- tapply(df$type, df$id, function(x) rle(x == "(K)"))
    df.new<- data.frame(id = names(temp), 
                    tally = unlist(lapply(temp, function(x) sum(x$values))))