我經常難以理解如何R在回圈內分配值。期望的行為對我來說似乎很簡單,但我顯然沒有很好地掌握R.
例如,我有一堆資料物件,我想為每個物件添加注釋(它們是不相關的,因此在此分配之外的串列中一起使用它們是沒有意義的)。這是一個 MWE
my_comment <- paste0("these objects were created on ", date())
obj1 <- "content1"
obj2 <- "content2"
obj_l <- list(obj1, obj2)
for(obj in obj_l) {
comment(obj) <- my_comment
}
## get 'NULL', but want "these objects were created ..."
comment(obj1)
## get 'NULL'
comment(obj_l)
## assignment is only made to temp variable 'obj'
## This makes sense, but not the desired outcome.
comment(obj)
我想解決方案將類似于以下偽代碼
obj_l <- c("obj1", "obj2")
for(name in obj_l)
unknown_function(name, comment, my_comment, unknown_args)
}
或者
modify(obj_l, my_comment, unknown_syntax)
如果我的偽代碼在軌道上,有人可以幫我解決這些問題unknown_嗎?
uj5u.com熱心網友回復:
這些中的任何一個都可以對#3 的組件進行注釋,obj_l或者在#3 的情況下生成一個添加了注釋的新串列。我們展示了 #1 的物件已更改,但其他物件相同。
# 1
obj1 <- "content1"
obj2 <- "content2"
obj_l <- list(obj1, obj2)
for(i in seq_along(obj_l)) {
comment(obj_l[[i]]) <- "some comment"
}
dput(obj_l)
## list(structure("content1", comment = "some comment"),
## structure("content2", comment = "some comment"))
# 2
obj1 <- "content1"
obj2 <- "content2"
obj_l <- mget(c("obj1", "obj2"))
for(nm in names(obj_l)) {
comment(obj_l[[nm]]) <- "some comment"
}
# 3
obj1 <- "content1"
obj2 <- "content2"
obj_l <- list(obj1, obj2)
obj_2 <- lapply(obj_l, `comment<-`, "some comment")
如果你想要評論obj1然后obj2
# 4
obj1 <- "content1"
obj2 <- "content2"
nms <- c("obj1", "obj2")
e <- .GlobalEnv # environment with objects
for(nm in nms) comment(e[[nm]]) <- "some comment"
# 5
obj1 <- "content1"
obj2 <- "content2"
obj_l <- mget(c("obj1", "obj2")) # named list w obj1, obj2 elements
lapply(obj_l, `comment<-`, "some comment") |> list2env(.GlobalEnv)
uj5u.com熱心網友回復:
使用回圈中的串列,通常更容易回圈索引或名稱,而不是實際的物件:
for(i in seq_along(obj_l)) {
comment(obj_l[[i]]) <- my_comment
}
comment(obj_l[[i]])
# [1] "these objects were created on Wed Nov 9 10:55:27 2022"
并且您已經為串列中的專案添加了評論,不要期望obj1有評論。(最好不要去想obj1,你所擁有的是obj_l有 2 個元素,obj_l[[1]]和obj_l[[2]].
uj5u.com熱心網友回復:
在閱讀了更多幫助頁面和反復試驗之后,這是我的解決方案
obj1 <- "content1"
obj2 <- "content2"
obj_l <- c("obj1", "obj2")
comment <- '"my comment!"'
for(x in obj_l) {
my_exp <- paste0("comment(", x, ") <- ", comment)
parse(text = my_exp)
}
comment(obj1)
# [1] "my comment"
uj5u.com熱心網友回復:
comment()我嘗試在不使用該功能的情況下回答實際問題。
見下文:
create_comment <- function(object, comment) {
new_object <- list(Object = object, Comment = comment)
return(new_object)
}
test <- map(c(obj1, obj2), create_comment, comment = my_comment)
我沒有使用該comment()函式,而是構建了一個為每個單獨物件創建一個命名串列物件的函式。這使得我可以創建一個包含所有存盤物件的大串列,并且它還將字串存盤my_comment在串列中。
測驗的輸出顯示...
[[1]]
[[1]]$Object
[1] "content1"
[[1]]$Comment
[1] "these objects were created on Wed Nov 09 16:04:04 2022"
[[2]]
[[2]]$Object
[1] "content2"
[[2]]$Comment
[1] "these objects were created on Wed Nov 09 16:04:04 2022"
然后我可以[[i]]用來訪問我的物件
例如:
test[[1]]
$Object
[1] "content1"
$Comment
[1] "these objects were created on Wed Nov 09 16:04:04 2022"
我可以通過使用進一步訪問我的個人評論或物件$
test[[1]]$Comment
[1] "these objects were created on Wed Nov 09 16:04:04 2022"
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/530808.html
標籤:r分配
