Reputation: 21
I'm trying to create summary stats from a list. I have mock data, created 3 sub-sets of data, saved it to csv and imported it, now i'm trying to calculate a mean for each column in each csv as a list.
examples_mean <- map(examples, function (data_df) {
data_df <- data_df %>%
summarise(vars(column1, column2), mean(mean))
return(data_df)
})
examples_mean
I tried using lapply() and various other suggestion from stackoverflow
but the only one that doesn't give an error/ warning is my code above, but returns this, below, which is what I need, but its missing the values in the mean column:
> examples_mean
$`data/example_1.csv`
# A tibble: 2 × 2
`vars(column1, column2)` `mean(mean)`
<quos> <dbl>
1 column1 NA
2 column2 NA
$`data/example_2.csv`
# A tibble: 2 × 2
`vars(column1, column2)` `mean(mean)`
<quos> <dbl>
1 column1 NA
2 column2 NA
$`data/example_3.csv`
# A tibble: 2 × 2
`vars(column1, column2)` `mean(mean)`
<quos> <dbl>
1 column1 NA
2 column2 NA
Upvotes: 0
Views: 470
Reputation: 887118
There are some syntax errors. If the intention is to loop over the columns column1
, column2
in each of the datasets in the list
, loop over the list
with map
, then select the column1
, column2
and loop across
those columns in summarise
to get the mean
of the column
library(purrr)
library(dplyr)
map(examples, ~
.x %>%
summarise(across(c(column1, column2), ~ mean(.x, na.rm = TRUE)))
)
Upvotes: 1