我想cleanfunction在我的環境中的所有 data.frames 上使用以下內容。
cleanfunction <- function(dataframe) {
dataframe <- as.data.frame(dataframe)
## get mode of all vars
var_mode <- sapply(dataframe, mode)
## produce error if complex or raw is found
if (any(var_mode %in% c("complex", "raw"))) stop("complex or raw not allowed!")
## get class of all vars
var_class <- sapply(dataframe, class)
## produce error if an "AsIs" object has "logical" or "character" mode
if (any(var_mode[var_class == "AsIs"] %in% c("logical", "character"))) {
stop("matrix variables with 'AsIs' class must be 'numeric'")
}
## identify columns that needs be coerced to factors
ind1 <- which(var_mode %in% c("logical", "character"))
## coerce logical / character to factor with `as.factor`
dataframe[ind1] <- lapply(dataframe[ind1], as.factor)
return(dataframe)
}
set.seed(10238)
DT = data.table(
A = rep(1:3, each = 5L),
B = rep(1:5, 3L),
C = sample(15L),
D = sample(15L)
)
DT_II <- copy(DT)
dfs <- ls()
現在我想將此功能應用于環境中的所有df。我已經嘗試了十件事,但我無法獲得正確的語法..
for (i in seq_along(dfs)) {
get(dfs[i])[ , lapply(.SD, cleanfunction)]
}
編輯:
我找到了這個解決方案,但它不存盤結果。
eapply(globalenv(), function(x) if (is.data.frame(x)) cleanfunction(x))
如何將結果存盤在每個物件中?
uj5u.com熱心網友回復:
您get(dfs[i])回傳對 a 的參考data.table,但隨后您正在lapply對該幀的每一列進行 -ing,我從函式引數推斷dataframe您期望一個完整的幀。可以從以下開始:
for (i in seq_along(dfs)) {
get(dfs[i])[ , cleanfunction(.SD)]
}
但是意識到這個操作回傳一個新的幀,它沒有使用規范data.table的機制來就地更新資料。我建議您更新您的功能以始終強制data.table并參考性地處理它。
cleanfunction <- function(dataframe) {
setDT(dataframe)
## get mode of all vars
var_mode <- sapply(dataframe, mode)
## produce error if complex or raw is found
if (any(var_mode %in% c("complex", "raw"))) stop("complex or raw not allowed!")
## get class of all vars
var_class <- sapply(dataframe, class)
## produce error if an "AsIs" object has "logical" or "character" mode
if (any(var_mode[var_class == "AsIs"] %in% c("logical", "character"))) {
stop("matrix variables with 'AsIs' class must be 'numeric'")
}
## identify columns that needs be coerced to factors
ind1 <- which(var_mode %in% c("logical", "character"))
## coerce logical / character to factor with `as.factor`
if (length(ind1)) dataframe[, c(ind1) := lapply(.SD, as.factor), .SDcols = ind1]
return(dataframe)
}
由于您當前的資料不會觸發任何更改,因此我將更新一個:
DT[,quux:="A"]
head(DT)
# A B C D quux
# <int> <int> <int> <int> <char>
# 1: 1 1 12 15 A
# 2: 1 2 4 6 A
# 3: 1 3 5 7 A
# 4: 1 4 9 1 A
# 5: 1 5 6 14 A
# 6: 2 1 15 13 A
for (i in seq_along(dfs)) cleanfunction(get(dfs[i]))
head(DT)
# A B C D quux
# <int> <int> <int> <int> <fctr>
# 1: 1 1 12 15 A
# 2: 1 2 4 6 A
# 3: 1 3 5 7 A
# 4: 1 4 9 1 A
# 5: 1 5 6 14 A
# 6: 2 1 15 13 A
請注意,for回圈僅依賴于參考更新;from 這里的回傳值cleanfunction被忽略。
這種方法完全是因為data.table參考語意。如果您正在使用data.frameor tbl_df,這可能需要將該呼叫包裝到cleanfunction(.)with assign(dfs[i], cleanfunction(..))。
uj5u.com熱心網友回復:
這對你有用嗎?:
# store all dataframes from environment a list
dfs <- Filter(function(x) is(x, "data.frame"), mget(ls()))
#then apply your function
lapply(dfs, cleanfunction)
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/461279.html
上一篇:C#使用For回圈顯示列舉名稱
