Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get multiple dataframes with a column containing frequency of elements in another column?

Tags:

list

dataframe

r

I have a list df_list with 40 dataframes (elements), where df_list[[1]] is the first dataframe of the list and so on.

id Date
2345 2022-12-01 12:52
4234 2022-12-01 12:52
2423 2022-12-01 12:53
2434 2022-12-01 12:53
3433 2022-12-01 12:53
5432 2022-12-01 13:54
4534 2022-12-01 14:55
3243 2022-12-01 15:55

What I expect (new dataframe):

Frequency Date
2 2022-12-01 12:52
3 2022-12-01 12:53
1 2022-12-01 13:54
2 2022-12-01 15:55

I would like to do this for each dataframe in the list and obtain a list of 'new' dataframes.

list <- list()
for (i in length(df_list)) {
list[[i]]  <- (count(df_list[[i]], "Date"))
name <- paste("Df",  myfiles_1, sep = "_")
result <- assign(name, list)

}

N.B. Date column is class POSIXlt.

like image 643
LZL Avatar asked Jan 19 '26 00:01

LZL


2 Answers

Given my_list with two data frames, we could:

Base R:

lapply(my_list, function(x) as.data.frame(table(x$Date)))

dplyr:

library(dplyr)
lapply(my_list, function(x) count(x, Date, name = "Frequency"))

purrr:

library(purrr)
map(my_list, count, Date, name = "Frequency")
$df1
              Date Frequency
1 2022-12-01 12:52         2
2 2022-12-01 12:53         3
3 2022-12-01 13:54         1
4 2022-12-01 14:55         1
5 2022-12-01 15:55         1

$df2
              Date Frequency
1 2022-12-01 12:42         2
2 2022-12-01 12:43         3
3 2022-12-01 13:44         1
4 2022-12-01 14:45         1
5 2022-12-01 15:45         1

list:

my_list <- list(df1 = structure(list(id = c(2345L, 4234L, 2423L, 2434L, 
3433L, 5432L, 4534L, 3243L), Date = c("2022-12-01 12:52", "2022-12-01 12:52", 
"2022-12-01 12:53", "2022-12-01 12:53", "2022-12-01 12:53", "2022-12-01 13:54", 
"2022-12-01 14:55", "2022-12-01 15:55")), class = "data.frame", row.names = c(NA, 
-8L)), df2 = structure(list(id = c(2345L, 4234L, 2423L, 2434L, 
3433L, 5432L, 4534L, 3243L), Date = c("2022-12-01 12:42", "2022-12-01 12:42", 
"2022-12-01 12:43", "2022-12-01 12:43", "2022-12-01 12:43", "2022-12-01 13:44", 
"2022-12-01 14:45", "2022-12-01 15:45")), class = "data.frame", row.names = c(NA, 
-8L)))
like image 58
TarJae Avatar answered Jan 20 '26 16:01

TarJae


Here's another dplyr solution using bind_rows and group_split:

my_list %>% 
  bind_rows(.id = "df_n") %>% 
  count(Date, df_n, name = "Frequency") %>% 
  group_split(df_n, .keep = FALSE)


#> [[1]]
#>   Date             Frequency
#>   <chr>                <int>
#> 1 2022-12-01 12:52         2
#> 2 2022-12-01 12:53         3
#> 3 2022-12-01 13:54         1
#> 4 2022-12-01 14:55         1
#> 5 2022-12-01 15:55         1
#> 
#> [[2]]
#>   Date             Frequency
#>   <chr>                <int>
#> 1 2022-12-01 12:42         2
#> 2 2022-12-01 12:43         3
#> 3 2022-12-01 13:44         1
#> 4 2022-12-01 14:45         1
#> 5 2022-12-01 15:45         1
like image 25
M-- Avatar answered Jan 20 '26 15:01

M--