firmo23
firmo23

Reputation: 8464

Use table() inside dataframes stored in a list to create new dataframes

I have the dataframe below:

library lubridate

eventtime<-c("2020-02-07 22:06:00","2020-02-07 22:00:00","2020-02-07 21:46:00")
eventvalue<-c("home","work",'exit')
geof<-data.frame(eventtime,eventvalue)

Then I use a list to store them and assign the list elements to different data.frames using:

library(lubridate)
library(tidyverse)

list1 <- geof %>%
  split(.$eventvalue) %>%
  bind_rows() %>%
  mutate(EventHour = hour(eventtime)) %>%
  split(.$eventvalue)

for (i in names(list1)) setNames(assign(i, data.frame(list1[[i]])), names(list1))

I would like then to use table() based on EventHour for each dataframe to create new dataframes with the frequency of each EventHour and then rename those columns

#table the events by count of hours
tablegeh<-data.frame(table(home$EventHour))

colnames(tablegeh)<-c("Hour","Frequency")

Upvotes: 0

Views: 22

Answers (1)

Allan Cameron
Allan Cameron

Reputation: 174596

You can use lapply:

lapply(list1, function(x) table(x$EventHour))
#> $exit
#> 21 
#>  1 
#>
#> $home
#> 22 
#>  1 
#>
#> $work
#> 22 
#>  1 

Obviously, you only have a single entry in each data frame, so the "tables" don't look much like tables!

Upvotes: 2

Related Questions