简体   繁体   中英

creating factor out of dummy variables and counting

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM