在Lapply中迭代时打印列表名称在3.2中不起作用 [英] print list names while iterating in lapply not working in 3.2
问题描述
我试图每次通过lapply
运行函数时输出列表名称.我较早前发布了这个问题,并由@提供了答案在我将R
升级到版本3.2.0之前,Ananda Mahto正常工作.它不再工作,并且出现以下错误消息:Error in eval.parent(quote(names(X)))[substitute(x)[[3]]] : invalid subscript type 'symbol'
x <- ts(rnorm(40,5), start = c(1961, 1), frequency = 12)
y <- ts(rnorm(50,20), start = c(1971, 1), frequency = 12)
z <- ts(rnorm(50,39), start = c(1981, 1), frequency = 12)
a <- ts(rnorm(50,59), start = c(1991, 1), frequency = 12)
dat.list <- list(x=x,y=y,z=z,a=a)
abc <- function(x) {
r <- mean(x)
print(eval.parent(quote(names(X)))[substitute(x)[[3]]])
return(r)
}
forl <- lapply(dat.list, abc)
我不确定问题是什么,但是我检查了新版本R
中的所有语法,没有任何改变.任何帮助是极大的赞赏.我也欢迎任何新想法.
如果稍微对其进行重组,则可以通过简单的外观获得相同的效果:
set.seed(42)
## using your dat.list construction code from above
abc <- function(x) { r <- mean(x); return(r); }
forl <- mapply(function(n, x) {
message(n)
abc(x)
}, names(dat.list), dat.list, SIMPLIFY=FALSE)
## x
## y
## z
## a
forl
## $x
## [1] 4.960464
## $y
## [1] 20.1141
## $z
## [1] 38.87175
## $a
## [1] 58.89825
我想您想让list
中的输出与vector
中的ergo SIMPLIFY=FALSE
相似,以模仿lapply
.否则,它是一个更简单的向量.
虽然您可以创建三明治函数,但必须重新实现lapply
功能,但这并不是通用的,因为您必须显式地传递名称和dat.
我在这里使用的个人编码偏好是使用message
而不是print
.理由是用户可以选择使用suppressMessages
(在mapply
之外,假设它可以/将被埋在函数中),而抑制print
调用的输出则需要更多的工作. /p>
I'm trying o output the list names every time I run the function thru lapply
. I posted this question earlier that I posted earlier, and the answer provided by @Ananda Mahto worked fine until I upgraded my R
to version 3.2.0. It is no longer working and I get the following error message: Error in eval.parent(quote(names(X)))[substitute(x)[[3]]] : invalid subscript type 'symbol'
x <- ts(rnorm(40,5), start = c(1961, 1), frequency = 12)
y <- ts(rnorm(50,20), start = c(1971, 1), frequency = 12)
z <- ts(rnorm(50,39), start = c(1981, 1), frequency = 12)
a <- ts(rnorm(50,59), start = c(1991, 1), frequency = 12)
dat.list <- list(x=x,y=y,z=z,a=a)
abc <- function(x) {
r <- mean(x)
print(eval.parent(quote(names(X)))[substitute(x)[[3]]])
return(r)
}
forl <- lapply(dat.list, abc)
I'm not sure what the issues is, but I checked all the syntax in the new version of R
nothing has changed. Any help is greatly appreciated. I'm open to any new ideas as well.
If you restructure it a little, you can get the same effect with a simpler appearance:
set.seed(42)
## using your dat.list construction code from above
abc <- function(x) { r <- mean(x); return(r); }
forl <- mapply(function(n, x) {
message(n)
abc(x)
}, names(dat.list), dat.list, SIMPLIFY=FALSE)
## x
## y
## z
## a
forl
## $x
## [1] 4.960464
## $y
## [1] 20.1141
## $z
## [1] 38.87175
## $a
## [1] 58.89825
I'm presuming you wanted the output in a list
vice a vector
, ergo SIMPLIFY=FALSE
to mimic lapply
. Otherwise, it's a simpler vector.
It's not as generic in that you have to explicitly pass the names as well as the dat, though you can create a sandwich function at the cost of having to reimplement lapply
functionality.
A personal coding preference I'm using here is the use of message
over print
. The rationale is that the user has the option to use suppressMessages
(outside the mapply
, assuming it could/would be buried in a function), whereas suppressing the output of a print
call is a bit more work.
这篇关于在Lapply中迭代时打印列表名称在3.2中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!