简体   繁体   English

R - 使用 purr 在数据框上添加列

[英]R - using purr add columns on dataframe

I'm looking to use purr to create a year variable on my data sets and join them together.我希望使用 purr 在我的数据集上创建一个年份变量并将它们连接在一起。 This example will illustrate the problem and what I have tired.这个例子将说明问题和我已经厌倦的地方。

# files in the directory 
files <- paste0("data/file_year_", 2015:2019, ".txt")

# map
files <- paste0("data_", 2013:2019, ".csv")
lst1 <-  files %>%
           map(~ read_csv(.x) %>%
             mutate(year = str_extract(., "\\d{4}")))

The list now needs to be merged into a single data frame.该列表现在需要合并到单个数据框中。

dat1 <- bind_rows(lst1, .id = 'grp')

However, I am getting the following error when I try to create the list:但是,当我尝试创建列表时出现以下错误:

Error in mutate_(.data, .dots = compat_as_lazy_dots(...)) : 
  argument ".data" is missing, with no default
In addition: Warning message:
In stri_extract_first_regex(string, pattern, opts_regex = opts(pattern)) :
  argument is not an atomic vector; coercing 

I tried searching for this error and nothing as helped so far.我尝试搜索此错误,但到目前为止没有任何帮助。 Any help would be greatly appreciated.任何帮助将不胜感激。

We can use map_df我们可以使用map_df

library(tidyverse)
map_df(files,~read.csv(.x) %>% mutate(year = str_extract(.x, "\\d{4}")),.id = "grp")

You can do this loop with data.table You can also perform specific operations on each file if you'd like.您可以使用data.table执行此循环 如果您愿意,您还可以对每个文件执行特定操作。

library(data.table)
lapply(list.files(pattern= paste0("data/file_year_", 2015:2016, ".txt")),
       function(x){
  your_files = fread(x)
  #your_files = somecode
})%>% rbindlist()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM