尝试读取多个 CSV 文件,然后汇总到有用的级别,但问题是某些日期是 YYYYMMDD(字符),其他日期是 DD-MM-YYYY(日期),因此汇总函数分别汇总这些日期。我尝试过 mutate 函数(我的代码如下),但它的结果是no applicable method for 'mutate_' applied to an object of class "list".
我也玩过 purrr 中的地图功能,但我不熟悉它,也无法让它工作。
sales_files <- list.files(path = "*folder redacted*", full.names = TRUE) %>%
lapply(read_csv) %>%
mutate(date = case_when(left(date,4) == "2020" ~ as.Date(as.character(date),format="%Y%m%d"), TRUE ~ date))
group_by(`ID`, `Date`) %>%
summarise(sales = sum(`Value`), quantity = sum(`Qty`)) %>%
bind_rows
Run Code Online (Sandbox Code Playgroud)
蒂亚!
尝试使用以下内容:
library(tidyverse)
library(lubridate)
output <- list.files(path = "*folder redacted*", full.names = TRUE) %>%
map_df(~{
#Read file name
read_csv(.x) %>%
#Convert different format date
mutate(date = parse_date_time(date, orders = c('Ymd', 'dmY'))) %>%
#Group by ID and Date
group_by(ID, Date) %>%
#Sum Value and Qty
summarise(sales = sum(Value), quantity = sum(Qty))
})
Run Code Online (Sandbox Code Playgroud)