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

fus*_*rqq 2 r dplyr purrr

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

\n

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

\n
\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n \n\n
参与者室温回复刺激状况
1112\'e\'星星.jpg全等
1150\'我\'钻石.jpg全等
\n
\n

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

\n
df <-   list.files(path = "C:/file_location", pattern = "*.csv") %>% map_df(~read_csv(.))\n
Run Code Online (Sandbox Code Playgroud)\n

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

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

但它不断返回错误

\n
Error in \'dplyr::bind_cols()\\\': ! Can\'t recycle \'..1\' (size 1400) to match\n\'..2\'(size 1425). Backtrace:\n1. ... %\\>% map_df(bind_cols)\n2. purrr::imap_dfc(., \\~read_csv(.) %\\>% mutate(Date_Created =\n   file.info(.x)$ctime))\n3. dplyr::bind_cols(res)\\\n
Run Code Online (Sandbox Code Playgroud)\n

我尝试将bind_cols 添加到imap_dfc,但仍然返回相同的错误。在寻找答案时,我发现dplyr 中存在某种问题,但我无法弄清楚如何使用此信息。

\n

(抱歉,格式很差,在某个地方我找不到问题,它实际上只想让我以某种方式发布)

\n

编辑: \n已经尝试了一些方法来解决这个问题,但是下面的方法可以工作,但会打印警告

\n
dfiles<-  list.files(path = "C:/file location", pattern = "*.csv", full.names = TRUE)\ndf<- purrr::map_df(L2ufiles, ~read_csv(.x)%>%\n            mutate(EndDate = file.info(.x)$ctime,\n                   rt=as.double(rt))) #needed to be added but seems to do nothing?\n
Run Code Online (Sandbox Code Playgroud)\n

哪个返回

\n
Warning: There was 1 warning in mutate().\n\xe2\x84\xb9 In argument: rt = as.double(rt).\nCaused by warning:\n! NAs introduced by coercion\n
Run Code Online (Sandbox Code Playgroud)\n

但数据框已构建完毕,足够接近了

\n

r2e*_*ans 5

我认为map_df这是正确的起点:

\n
    \n
  • imap_*将为内部函数提供两个参数:值(文件名)本身为.y,它们的名称(如果已定义)或整数序列为.x;您不需要整数,也不需要名称,所以这是不合适的;
  • \n
  • *map_dfc通过添加列而不是添加行来组合,我认为您需要后者。
  • \n
\n

我有一些文件,我将从n_max=2每个文件中读取两行 ( ) 进行演示(不要使用n_max自己):

\n
library(dplyr)\n\nfiles <- list.files("~/Downloads/", pattern="mt.*csv$", full.names=TRUE)\n\npurrr::map_df(files, ~ read_csv(.x, n_max=2) %>%\n    mutate(filesize = file.info(.x)$ctime))\n# Rows: 2 Columns: 11\n# \xe2\x94\x80\xe2\x94\x80 Column specification \xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\n# Delimiter: ","\n# dbl (11): mpg, cyl, disp, hp, drat, wt, qsec, vs, am, gear, carb\n# \xe2\x84\xb9 Use `spec()` to retrieve the full column specification for this data.\n# \xe2\x84\xb9 Specify the column types or set `show_col_types = FALSE` to quiet this message.\n# Rows: 2 Columns: 11\n# \xe2\x94\x80\xe2\x94\x80 Column specification \xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\n# Delimiter: ","\n# dbl (11): mpg, cyl, disp, hp, drat, wt, qsec, vs, am, gear, carb\n# \xe2\x84\xb9 Use `spec()` to retrieve the full column specification for this data.\n# \xe2\x84\xb9 Specify the column types or set `show_col_types = FALSE` to quiet this message.\n# Rows: 2 Columns: 11\n# \xe2\x94\x80\xe2\x94\x80 Column specification \xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\n# Delimiter: ","\n# dbl (11): mpg, cyl, disp, hp, drat, wt, qsec, vs, am, gear, carb\n# \xe2\x84\xb9 Use `spec()` to retrieve the full column specification for this data.\n# \xe2\x84\xb9 Specify the column types or set `show_col_types = FALSE` to quiet this message.\n# # A tibble: 6 \xc3\x97 12\n#     mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb filesize           \n#   <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dttm>             \n# 1   321     6   160   110   3.9  2.62  16.5     0     1     4     4 2023-07-27 09:55:35\n# 2   321     6   160   110   3.9  2.88  17.0     0     1     4     4 2023-07-27 09:55:35\n# 3   321     6   160   110   3.9  2.62  16.5     0     1     4     4 2023-07-27 10:02:41\n# 4   321     6   160   110   3.9  2.88  17.0     0     1     4     4 2023-07-27 10:02:41\n# 5    21     6   160   110   3.9  2.62  16.5     0     1     4     4 2023-07-27 09:48:33\n# 6    21     6   160   110   3.9  2.88  17.0     0     1     4     4 2023-07-27 09:48:33\n
Run Code Online (Sandbox Code Playgroud)\n

另外,map_df被取代来代替mapand 或purrr::list_rbindor dplyr::bind_rows(以下两者是等效的):

\n
purrr::map(files, ~ read_csv(.x, n_max=2) %>%\n    mutate(filesize = file.info(.x)$ctime)) %>%\n  purrr::list_rbind()\n\npurrr::map(files, ~ read_csv(.x, n_max=2) %>%\n    mutate(filesize = file.info(.x)$ctime)) %>%\n  bind_rows()\n
Run Code Online (Sandbox Code Playgroud)\n