简体   繁体   English

如何使用r purrr:map创建多个相同的对象(如数据框)

[英]How to use r purrr:map to create multiple of the same object (such as data frame)

I want to know to what extent it is possible to use purrr's mapping functions to create objects in general, though at the moment and with the example below I'm looking at data frames. 我想知道在多大程度上可以使用purrr的映射函数来创建一般的对象,尽管目前我正在查看数据框。

A<-seq(1:5)
B<-seq(6:10)
C<-c("x","y","x","y","x")
dat<data.frame(A,B,C)
cols<-names(dat)

create_df<-function(x) {
    x<- dat[x]
    return(x)
}
A<-create_df("A")

This will create a data frame called A with column A from dat. 这将创建一个名为A的数据框,其中包含来自dat的列A. I want to create data frames A/B/C, each with one column. 我想创建数据框A / B / C,每个都有一列。 I have tried different ways of specifying the .f argument as well as different map functions (map, map2, map_dfc, etc.). 我尝试了不同的方法来指定.f参数以及不同的map函数(map,map2,map_dfc等)。 My original best guess: 我最初的猜测是:

map(.x=cols,~create_df(.x))    

Clarification: I am asking for help because all of the specifications of map that I have tried have given an error. 澄清:我正在寻求帮助,因为我尝试过的所有地图规格都给出了错误。

Code that worked: 有效的代码:

map(names(dat), ~assign(.x, dat[.x], envir = .GlobalEnv))

This creates A/B/C as data frames and prints to the console (which I don't need but does not bother me for now). 这会创建A / B / C作为数据框并打印到控制台(我不需要但现在不打扰我)。

We can use split from base R to get a list of one column data.frame s 我们可以使用从base R split来获得一列data.frame s的list

lst <- split.default(dat, names(dat))

It is better to keep it in a list , but if the intention is to have multiple objects in the global environment 最好将它保存在list ,但是如果打算在全局环境中拥有多个对象

list2env(lst, envir = .GlobalEnv)

Using the purrr package, I think your custom function is not necessary. 使用purrr包,我认为您的自定义功能不是必需的。 The function includes a reference to the data, which is not optimal (especially if it doesn't exist in the environment). 该函数包括对数据的引用,这不是最佳的(特别是如果它在环境中不存在)。

to return as a list of single column dataframes: 作为单列数据帧列表返回:

cols<-names(dat)
map(cols, ~dat[.x])

or alternatively: map(names(dat), ~dat[.x]) 或者: map(names(dat), ~dat[.x])

returns: 收益:

[[1]]
# A tibble: 5 x 1
      A
  <int>
1     1
2     2
3     3
4     4
5     5

[[2]]
# A tibble: 5 x 1
      B
  <int>
1     1
2     2
3     3
4     4
5     5

[[3]]
# A tibble: 5 x 1
  C    
  <chr>
1 x    
2 y    
3 x    
4 y    
5 x    

If you want to stick with tidyverse principles, you can store them within a dataframe as a list-column. 如果您想坚持使用tidyverse原则,可以将它们作为列表列存储在数据框中。

dfs <-
  data_frame(column = cols) %>% 
    mutate(data = map(cols, ~dat[.x]))

# A tibble: 3 x 2
  column data            
  <chr>  <list>          
1 A      <tibble [5 x 1]>
2 B      <tibble [5 x 1]>
3 C      <tibble [5 x 1]>

You can pull out individual data as needed: 您可以根据需要提取单个数据:

B <- dfs$data[[2]]  

# A tibble: 5 x 1
      B
  <int>
1     1
2     2
3     3
4     4
5     5

Along the lines of your original suggestion, here's an alternative function that uses purrr:map within it. 根据您的原始建议,这里是一个使用purrr:map的替代函数。 I'm not sure how good of an idea this is, but maybe it has a use: 我不确定这是一个多么好的想法,但它可能有用:

create_objects_from_df <- function(dat) {
  map(names(dat), ~assign(.x, dat[.x], envir = .GlobalEnv))
  }
create_objects_from_df(dat)

This creates the objects in your global environment, as individual objects with the column names. 这将在全局环境中创建对象,作为具有列名称的单个对象。

暂无
暂无

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

相关问题 如何在R中使用purrr :: map() - How to use the purrr::map() in R 使用 purrr::map 将 tibble 中的列表与数据框合并 - Use purrr::map to merge list within a tibble with a data frame 如何使用循环 | 申请 | map 为多个变量值分割数据帧并在 r 中创建多个统计摘要() - how to use a loop | apply | map to slice a data-frame for multiple variable values and create multiple statistics summary() in r 如何使用 **purrr** pacakge 中的 `map` 系列命令在数据框中的行之间交换列? - how to use the `map` family command in **purrr** pacakge to swap the columns across rows in data frame? 如何使用purrr :: pmap在nested.data.frame中绘制多个ggplot - How to use purrr::pmap to plot multiple ggplot in nested.data.frame R 中的 purrr 包:如何对通过 map() 应用于嵌套数据的函数使用不断变化的参数 - purrr package in R: How to use a changing parameter for a function that is applied to nested data with map() 如何使用 purrr 中的 map 和 dplyr::mutate 根据列对创建多个新列 - How to use map from purrr with dplyr::mutate to create multiple new columns based on column pairs 在 R/Shiny 中如何使用 purrr::map 创建其他面板 - In R/Shiny how to create others panels with purrr::map 如何使用 purrr::map family 将函数直接应用于数据框列表,而不是创建新对象 - How use purrr::map family to apply function to a list of data frames directly, not create new objects R:创建数据框,每个节点的边缘都喜欢相同的 object - R: create data frame with the edges of each node liking the same object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM