使用map_df R从文件创建数据时将文件创建日期添加为列

问题描述 投票:0回答:2

我很难弄清楚如何将文件创建日期作为一列添加到我正在使用map_df构建的数据框中(此代码是给我的,所以我很迷失)。

我有一个包含几个 csv 文件的文件夹,它们的组织方式大致如下

参与者 rt 回应 刺激 状况
1 112 'e' 星星.jpg 一致
1 150 '我' 钻石.jpg 一致

对于我像这样一起进行的实验..

df <-   list.files(path = "C:/file_location", pattern = "*.csv") %>% map_df(~read_csv(.))

我意识到,如果我想添加文件创建日期,我必须在此时添加它,所以我尝试过

df <- list.files(path = "C:/data location/", pattern = "*.csv") %>%      
    imap_dfc(~read_csv(.)%>%             
    mutate(Date_Created = file.info(.x)$ctime))

但它不断返回错误

Error in 'dplyr::bind_cols()\': ! Can't recycle '..1' (size 1400) to match
'..2'(size 1425). Backtrace:
1. ... %\>% map_df(bind_cols)
2. purrr::imap_dfc(., \~read_csv(.) %\>% mutate(Date_Created =
   file.info(.x)$ctime))
3. dplyr::bind_cols(res)\

我尝试将bind_cols添加到imap_dfc,但仍然返回相同的错误。在寻找答案时,我发现 dplyr 中存在某种问题,但我无法弄清楚如何使用此信息。 (很抱歉格式很差,在某个地方我找不到问题,它真的只想让我以某种方式发布)

r dplyr purrr
2个回答
0
投票

library(dplyr) files <- list.files("~/Downloads/", pattern="mt.*csv$", full.names=TRUE) purrr::map_df(files, ~ read_csv(.x, n_max=2) %>% mutate(filesize = file.info(.x)$size)) # Rows: 2 Columns: 11 # ── Column specification ─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── # Delimiter: "," # dbl (11): mpg, cyl, disp, hp, drat, wt, qsec, vs, am, gear, carb # ℹ Use `spec()` to retrieve the full column specification for this data. # ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message. # Rows: 2 Columns: 11 # ── Column specification ─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── # Delimiter: "," # dbl (11): mpg, cyl, disp, hp, drat, wt, qsec, vs, am, gear, carb # ℹ Use `spec()` to retrieve the full column specification for this data. # ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message. # Rows: 2 Columns: 11 # ── Column specification ─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── # Delimiter: "," # dbl (11): mpg, cyl, disp, hp, drat, wt, qsec, vs, am, gear, carb # ℹ Use `spec()` to retrieve the full column specification for this data. # ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message. # # A tibble: 6 × 12 # mpg cyl disp hp drat wt qsec vs am gear carb filesize # <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> # 1 321 6 160 110 3.9 2.62 16.5 0 1 4 4 1335 # 2 321 6 160 110 3.9 2.88 17.0 0 1 4 4 1335 # 3 321 6 160 110 3.9 2.62 16.5 0 1 4 4 1335 # 4 321 6 160 110 3.9 2.88 17.0 0 1 4 4 1335 # 5 21 6 160 110 3.9 2.62 16.5 0 1 4 4 1303 # 6 21 6 160 110 3.9 2.88 17.0 0 1 4 4 1303



0
投票

© www.soinside.com 2019 - 2024. All rights reserved.