-1

I would like to count certain things in my dataset. I have panel data and ideally would like to count the number of activities per zip.

zip <- c(1,1,1,2,2,3,3,4,4,5,5)
    activity <- c(1,1,1,2,2,3,4,5,5,6,6)
    completion <- c(0,0,1,0,1,1,1,0,0,0,1)

So my output would tell me that person 4 has 2 tasks.

zip 1
    frequency activity 2

I use this data and with this function it works perfectly.

library(dplyr)
    cllw %>% 
      group_by(zip) %>% 
      summarise("id_task" = n())%>% View()

Now, i coded some dummy variables for the zip data like this:

df$California <- ifelse(df$zip ==1, 1, 0)
df$Hawaii <- ifelse(df$zip ==2, 1, 0)
df$Oregon <- ifelse(df$zip ==3, 1, 0)
df$Washington <- ifelse(df$zip ==4, 1, 0)
df$Alaska <- ifelse(df$zip ==5, 1, 0)

Now, a few days ago i just run the same code as above after adding the dummys to my df in order to not only get zip-level but state-level results.

so the output would look like this

California
frequency activity 2

How would i be able to get the state-level effects into my function

Lisam
  • 31
  • 7
  • `df$zip = 1` is always going to be trueish, since you're *assigning* not *comparing*; perhaps you meant `df$zip == 1`? – r2evans Jun 19 '20 at 23:00
  • thank you, corrected it. Volatile error – Lisam Jun 19 '20 at 23:05
  • long-data can often be preferred, are you using the state-columns as dummy variables for logistic regression, or are they just a step to assign states to zips? – r2evans Jun 19 '20 at 23:12
  • One improvement would be using `left_join(dat, statezips, by = "zip")` to assign stages to each of your zips, where `statezips <- tibble(state = c("california", ...), zip = c(1L, ...))`. – r2evans Jun 19 '20 at 23:13

1 Answers1

0

Do you need something like this?

library(dplyr)

df %>%
  tidyr::pivot_longer(cols = California:Alaska) %>%
  filter(value == 1) %>%
  count(name)

# A tibble: 5 x 2
#  name           n
#  <chr>      <int>
#1 Alaska         2
#2 California     3
#3 Hawaii         2
#4 Oregon         2
#5 Washington     2
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213