r - r: Assign n dataframe subsets from a list created in a custom function to objects in global environment
问题描述
It's my first try with lists. I try to clean up my code and put some code in functions. One idea is to subset a big dataframe in multiple subsets with a function. So I can call the subset with a function when needed.
With the mtcars dataframe I would like to explain what I am trying to do:
- add an id to mtcars
- create a function with one argument (mtcars) that outputs a list of subset dataframes (mtcars1, mtcars2, mtcars3) -> learned here: How to assign from a function which returns more than one value? answer by Federico Giorgi
What I achieve is to create the list. But when it comes to see the 3 subset dataframe objects (mtcars1, mtcars2, mtcars3) in the global environment my knowledge is ending. So how can I call these 3 dataframe objects from the list with my function. Thanks!
My Code:
library(dplyr)
# add id to mtcars
mtcars <- mtcars %>%
mutate(id = row_number())
# create function to subset in 3 dataframes
my_func_cars <- function(input){
# first subset
mtcars1 <- mtcars %>%
select(id, mpg, cyl, disp)
# second subset
mtcars2 <- mtcars %>%
select(id, hp, drat, wt, qsec)
# third subset
mtcars3 <- mtcars %>%
select(id, vs, am, gear, carb)
output <- list(mtcars1, mtcars2, mtcars3)
return(output)
}
output<-my_func_cars(mtcars)
for (i in output) {
print(i)
}
解决方案
It may be better to output a named list
library(dplyr)
library(stringr)
my_func_cars <- function(input){
nm1 <- deparse(substitute(input))
# first subset
obj1 <- input %>%
select(id, mpg, cyl, disp)
# second subset
obj2 <- input %>%
select(id, hp, drat, wt, qsec)
# third subset
obj3 <- input %>%
select(id, vs, am, gear, carb)
dplyr::lst(!! str_c(nm1, 1) := obj1,
!! str_c(nm1, 2) := obj2,
!! str_c(nm1, 3) := obj3)
}
and then we use list2env
to create objects in the global env
mtcars <- mtcars %>%
mutate(id = row_number())
list2env(my_func_cars(mtcars), .GlobalEnv)
-check the objects
head(mtcars1, 2)
# id mpg cyl disp
#1 1 21 6 160
#2 2 21 6 160
head(mtcars2, 2)
# id hp drat wt qsec
#1 1 110 3.9 2.620 16.46
#2 2 110 3.9 2.875 17.02
head(mtcars3, 2)
# id vs am gear carb
#1 1 0 1 4 4
#2 2 0 1 4 4
推荐阅读
- git - git 显示文件内容和文件末尾的 LF 或 CRLF
- .net - 是否可以在 Elasticsearch 路径中使用 lambda 表达式?
- swiftui - SourceKitService & swift 消耗 100% CPU 和百兆内存
- eclipse - 有人可以告诉我这个装饰器/装饰在 Eclipse 中用于同步/传入模式是什么吗?
- google-apps-script - 如何使用 insertCheckboxes 和 TRUE/FALSE 创建 onEdit 函数?
- angular - 在日期选择器中限制日期时,它会限制明年的日期
- javascript - 用户切换时重新加载浏览器窗口
- java - 将 cosmosDB sdk 与 java 一起使用时出现 ALLOW_TRAILING_COMMA 错误
- angular - *ngIf 不能在 *ng 内部工作,用于渲染空元素?离子的
- mysql - 为什么 my.ini 文件中的更改不影响?