简体   繁体   English

R如何从列表中提取数据集?

[英]R how to extract datasets from a list?

I have the following dataframes that are stored in a list as a result of using the map() function:由于使用 map() 函数,我将以下数据帧存储在列表中:

在此处输入图片说明

How can I extract the six dataframes from the list?如何从列表中提取六个数据框? I would like to do this because I would like to give each column a different name of the dataframe and then store all data in a csv file?我想这样做是因为我想给每列一个不同的数据框名称,然后将所有数据存储在一个 csv 文件中? Or do I not have to extract the dfs from the list then?或者我不必从列表中提取 dfs 吗?

You have a few options你有几个选择

Fake data假数据

library(tidyverse)

df <- tibble(a = 1:9,b = letters[1:9])


x <- list(df,df,df,df)

You can bind dfs and create just one您可以绑定 dfs 并只创建一个

bind_rows(x)

You can execute your logic on all dfs您可以在所有 dfs 上执行您的逻辑

logic <- . %>% 
  mutate(c = a*3)

x %>% map(logic)

You can can also name the dfs inside the list您还可以命名列表中的 dfs

names(x) <- letters[1:4]

bind_rows(x,.id = "id")

I am not sure about what you are exactly looking for, so below are something just from guessing your objective:我不确定你到底在寻找什么,所以下面只是猜测你的目标:

  • If you want to extract the data frame as objects in your global environment, then you can do like this:如果要将数据框提取为全局环境中的对象,则可以这样做:
list2env(setNames(dats1,paste0("df",seq(dats1))),envir = .GlobalEnv)
  • Assuming you are giving names "col1" and "col2" to two columns of different data frames in your list, maybe this can help you假设您将名称"col1""col2"命名为列表中不同数据框的两列,也许这可以帮助您
dats1 <- lapply(dats1, setNames, c("col1","col2"))

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

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