我知道deparse +替代技巧從作爲參數傳遞給函數的對象中獲取名稱,但循環中的相同技巧無效。獲取循環內對象的名稱
我的代碼(只是用於測試):
mylist <- list(first = c("lawyer","janitor"), second = c("engineer","housewife"))
for (element in names(mylist)){
print(deparse(substitute(mylist[[element]])))
}
[1] "mylist[[element]]"
[1] "mylist[[element]]"
有沒有得到使用for
循環按照使用lapply
lapply(mylist, function(x) { print(names(x))})
# NULL
# NULL
# $first
# NULL
#
# $second
# NULL
結果?:
first
second
在您的例子,你可以只用'打印(元)'.. –
這有什麼錯'名字(MYLIST)'? –