获取使用purrr::map创建的列表项的名称 [英] Get the name of a list item created with purrr::map

查看:9
本文介绍了获取使用purrr::map创建的列表项的名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用purrr::map检索了CSV文件列表,并获得了一个很大的列表。

  csv_files <- list.files(path = data_path, pattern = '\.csv$', full.names = TRUE)
  all_csv <- purrr::map(csv_files, readr::read_csv2)
  names(all_csv) <- gsub(data_path, "", csv_files)
  return all_csv

按@Spacedman建议编辑

我还需要在process_csv_data函数中分别处理每个Tibble/Data帧。

purrr::map(all_csv, process_csv_data)

如何在没有for循环的情况下检索大列表中单个项目的名称?

推荐答案

使用map2,如下例所示:

> L = list(a=1:10, b=1:5, c=1:6)
> map2(L, names(L), function(x,y){message("x is ",x," y is ",y)})
x is 12345678910 y is a
x is 12345 y is b
x is 123456 y is c

message在函数中作为x的列表输出被message屏蔽,但它是L的列表元素。

这篇关于获取使用purrr::map创建的列表项的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆