[英]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.