我想撰寫一個函式,將 df 按多個因子變數(一次一個)拆分,然后在結果串列上運行另一個函式。但是,我找不到合適的方法來呼叫這個因素base::split
這是我到目前為止嘗試過的
library (tidyverse)
fun_res <- function (x,y) {
list_temp <- base::split (x, x$y, drop = FALSE)
lapply (list_temp, another_fun) # does another function and returns results in a list
}
然后我想運行fun_res以按各種因子列拆分 df
fun_res_(df, factor_col1)
fun_res_(df, factor_col2)
但是,x$y導致以下錯誤Error in split.default(x = seq_len(nrow(x)), f = f, drop = drop, ...) : group length is 0 but data length > 0
什么是正確的方法呢?
這是一個簡短的代表:
library (tidyverse)
data1 <- c(1,2,3,4,1,2,3,4)
data2 <- c(4,3,2,1,4,3,2,1)
factor1 <- c(rep(1,4), rep(2,4)) %>% as.factor ()
factor2 <- c(rep(1,5), rep(2,3)) %>% as.factor ()
df <- data.frame (data1, data2, factor1, factor2)
fun_res <- function (x,y) {
list_temp <- base::split (x, x$y, drop = FALSE)
lapply (list_temp, function (z){ # just a random function
as.list(z) %>%
return ()
})
}
fun_res(df, factor1)
fun_res(df, factor2)
我想fun_res按順序呼叫每個因素的原因是,對于我的真實資料,函式lapply回傳一個統計測驗結果串列,我想通過分別參考每個結果串列來列印這些結果。
uj5u.com熱心網友回復:
在base R中,如果我們傳遞不帶引號的引數,請使用substituteanddeparse它到字符,然后使用該列子集[[
fun_res <- function (x,y) {
y <- deparse(substitute(y))
list_temp <- base::split (x, x[[y]], drop = FALSE)
list_temp
}
-測驗
> fun_res(df, factor1)
$`1`
data1 data2 factor1 factor2
1 1 4 1 1
2 2 3 1 1
3 3 2 1 1
4 4 1 1 1
$`2`
data1 data2 factor1 factor2
5 1 4 2 1
6 2 3 2 2
7 3 2 2 2
8 4 1 2 2
> fun_res(df, factor2)
$`1`
data1 data2 factor1 factor2
1 1 4 1 1
2 2 3 1 1
3 3 2 1 1
4 4 1 1 1
5 1 4 2 1
$`2`
data1 data2 factor1 factor2
6 2 3 2 2
7 3 2 2 2
8 4 1 2 2
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/430466.html
下一篇:沒有變數名的向量的值
