是否有一种方法可以在我的lapply()函数中获得列表索引名?
n = names(mylist)
lapply(mylist, function(list.elem) { cat("What is the name of this list element?\n" })
我以前问过是否可以在lapply()返回的列表中保留索引名,但我仍然不知道是否有一种简单的方法来获取自定义函数中的每个元素名。我希望避免对名称本身调用lapply,我宁愿在函数参数中获得名称。
是否有一种方法可以在我的lapply()函数中获得列表索引名?
n = names(mylist)
lapply(mylist, function(list.elem) { cat("What is the name of this list element?\n" })
我以前问过是否可以在lapply()返回的列表中保留索引名,但我仍然不知道是否有一种简单的方法来获取自定义函数中的每个元素名。我希望避免对名称本身调用lapply,我宁愿在函数参数中获得名称。
当前回答
@caracals和@Tommy都是很好的解决方案,这是一个包括list ' s和data.frame ' s的例子。 R是list´s和data.frame´s (dput(R[[1]]在末尾)的列表。
names(r)
[1] "todos" "random"
r[[1]][1]
$F0
$F0$rst1
algo rst prec rorac prPo pos
1 Mean 56.4 0.450 25.872 91.2 239
6 gbm1 41.8 0.438 22.595 77.4 239
4 GAM2 37.2 0.512 43.256 50.0 172
7 gbm2 36.8 0.422 18.039 85.4 239
11 ran2 35.0 0.442 23.810 61.5 239
2 nai1 29.8 0.544 52.281 33.1 172
5 GAM3 28.8 0.403 12.743 94.6 239
3 GAM1 21.8 0.405 13.374 68.2 239
10 ran1 19.4 0.406 13.566 59.8 239
9 svm2 14.0 0.385 7.692 76.2 239
8 svm1 0.8 0.359 0.471 71.1 239
$F0$rst5
algo rst prec rorac prPo pos
1 Mean 52.4 0.441 23.604 92.9 239
7 gbm2 46.4 0.440 23.200 83.7 239
6 gbm1 31.2 0.416 16.421 79.5 239
5 GAM3 28.8 0.403 12.743 94.6 239
4 GAM2 28.2 0.481 34.815 47.1 172
11 ran2 26.6 0.422 18.095 61.5 239
2 nai1 23.6 0.519 45.385 30.2 172
3 GAM1 20.6 0.398 11.381 75.7 239
9 svm2 14.4 0.386 8.182 73.6 239
10 ran1 14.0 0.390 9.091 64.4 239
8 svm1 6.2 0.370 3.584 72.4 239
目标是取消所有列表,将列表名称的序列作为列来标识情况。
r=unlist(unlist(r,F),F)
names(r)
[1] "todos.F0.rst1" "todos.F0.rst5" "todos.T0.rst1" "todos.T0.rst5" "random.F0.rst1" "random.F0.rst5"
[7] "random.T0.rst1" "random.T0.rst5"
取消列表,但不要删除data.frame。
ra=Reduce(rbind,Map(function(x,y) cbind(case=x,y),names(r),r))
Map将名称序列作为一列。减少连接所有data.frame。
head(ra)
case algo rst prec rorac prPo pos
1 todos.F0.rst1 Mean 56.4 0.450 25.872 91.2 239
6 todos.F0.rst1 gbm1 41.8 0.438 22.595 77.4 239
4 todos.F0.rst1 GAM2 37.2 0.512 43.256 50.0 172
7 todos.F0.rst1 gbm2 36.8 0.422 18.039 85.4 239
11 todos.F0.rst1 ran2 35.0 0.442 23.810 61.5 239
2 todos.F0.rst1 nai1 29.8 0.544 52.281 33.1 172
注:r [[1]]:
structure(list(F0 = structure(list(rst1 = structure(list(algo = c("Mean",
"gbm1", "GAM2", "gbm2", "ran2", "nai1", "GAM3", "GAM1", "ran1",
"svm2", "svm1"), rst = c(56.4, 41.8, 37.2, 36.8, 35, 29.8, 28.8,
21.8, 19.4, 14, 0.8), prec = c(0.45, 0.438, 0.512, 0.422, 0.442,
0.544, 0.403, 0.405, 0.406, 0.385, 0.359), rorac = c(25.872,
22.595, 43.256, 18.039, 23.81, 52.281, 12.743, 13.374, 13.566,
7.692, 0.471), prPo = c(91.2, 77.4, 50, 85.4, 61.5, 33.1, 94.6,
68.2, 59.8, 76.2, 71.1), pos = c(239L, 239L, 172L, 239L, 239L,
172L, 239L, 239L, 239L, 239L, 239L)), .Names = c("algo", "rst",
"prec", "rorac", "prPo", "pos"), row.names = c(1L, 6L, 4L, 7L,
11L, 2L, 5L, 3L, 10L, 9L, 8L), class = "data.frame"), rst5 = structure(list(
algo = c("Mean", "gbm2", "gbm1", "GAM3", "GAM2", "ran2",
"nai1", "GAM1", "svm2", "ran1", "svm1"), rst = c(52.4, 46.4,
31.2, 28.8, 28.2, 26.6, 23.6, 20.6, 14.4, 14, 6.2), prec = c(0.441,
0.44, 0.416, 0.403, 0.481, 0.422, 0.519, 0.398, 0.386, 0.39,
0.37), rorac = c(23.604, 23.2, 16.421, 12.743, 34.815, 18.095,
45.385, 11.381, 8.182, 9.091, 3.584), prPo = c(92.9, 83.7,
79.5, 94.6, 47.1, 61.5, 30.2, 75.7, 73.6, 64.4, 72.4), pos = c(239L,
239L, 239L, 239L, 172L, 239L, 172L, 239L, 239L, 239L, 239L
)), .Names = c("algo", "rst", "prec", "rorac", "prPo", "pos"
), row.names = c(1L, 7L, 6L, 5L, 4L, 11L, 2L, 3L, 9L, 10L, 8L
), class = "data.frame")), .Names = c("rst1", "rst5")), T0 = structure(list(
rst1 = structure(list(algo = c("Mean", "ran1", "GAM1", "GAM2",
"gbm1", "svm1", "nai1", "gbm2", "svm2", "ran2"), rst = c(22.6,
19.4, 13.6, 10.2, 9.6, 8, 5.6, 3.4, -0.4, -0.6), prec = c(0.478,
0.452, 0.5, 0.421, 0.423, 0.833, 0.429, 0.373, 0.355, 0.356
), rorac = c(33.731, 26.575, 40, 17.895, 18.462, 133.333,
20, 4.533, -0.526, -0.368), prPo = c(34.4, 52.1, 24.3, 40.7,
37.1, 3.1, 14.4, 53.6, 54.3, 116.4), pos = c(195L, 140L,
140L, 140L, 140L, 195L, 195L, 140L, 140L, 140L)), .Names = c("algo",
"rst", "prec", "rorac", "prPo", "pos"), row.names = c(1L,
9L, 3L, 4L, 5L, 7L, 2L, 6L, 8L, 10L), class = "data.frame"),
rst5 = structure(list(algo = c("gbm1", "ran1", "Mean", "GAM1",
"GAM2", "svm1", "nai1", "svm2", "gbm2", "ran2"), rst = c(17.6,
16.4, 15, 12.8, 9, 6.2, 5.8, -2.6, -3, -9.2), prec = c(0.466,
0.434, 0.435, 0.5, 0.41, 0.8, 0.44, 0.346, 0.345, 0.337),
rorac = c(30.345, 21.579, 21.739, 40, 14.754, 124, 23.2,
-3.21, -3.448, -5.542), prPo = c(41.4, 54.3, 35.4, 22.9,
43.6, 2.6, 12.8, 57.9, 62.1, 118.6), pos = c(140L, 140L,
195L, 140L, 140L, 195L, 195L, 140L, 140L, 140L)), .Names = c("algo",
"rst", "prec", "rorac", "prPo", "pos"), row.names = c(5L,
9L, 1L, 3L, 4L, 7L, 2L, 8L, 6L, 10L), class = "data.frame")), .Names = c("rst1",
"rst5"))), .Names = c("F0", "T0"))
其他回答
不幸的是,lapply只给出你传递给它的向量的元素。 通常的解决方法是向它传递向量的名称或索引,而不是向量本身。
但请注意,你总是可以传递额外的参数给函数,所以下面的工作:
x <- list(a=11,b=12,c=13) # Changed to list to address concerns in commments
lapply(seq_along(x), function(y, n, i) { paste(n[[i]], y[[i]]) }, y=x, n=names(x))
这里我在x的下标上使用lapply,但也传递了x和x的名称。正如你所看到的,函数参数的顺序可以是任何东西——lapply将“元素”(这里是索引)传递给额外的参数中未指定的第一个参数。在这种情况下,我指定了y和n,所以只剩下I了……
这会产生以下结果:
[[1]]
[1] "a 11"
[[2]]
[1] "b 12"
[[3]]
[1] "c 13"
更简单的例子,相同的结果:
lapply(seq_along(x), function(i) paste(names(x)[[i]], x[[i]]))
这里,函数使用“全局”变量x并在每次调用中提取名称。
同样的问题我已经遇到过很多次了…… 我开始用另一种方法…我不再使用lapply,而是开始使用mapply
n = names(mylist)
mapply(function(list.elem, names) { }, list.elem = mylist, names = n)
汤米的答案适用于命名向量,但我知道你对列表感兴趣。似乎他在绕一圈因为他引用了调用环境中的x。这个函数只使用传递给函数的参数,因此不假设传递对象的名称:
x <- list(a=11,b=12,c=13)
lapply(x, function(z) { attributes(deparse(substitute(z)))$names } )
#--------
$a
NULL
$b
NULL
$c
NULL
#--------
names( lapply(x, function(z) { attributes(deparse(substitute(z)))$names } ))
#[1] "a" "b" "c"
what_is_my_name <- function(ZZZ) return(deparse(substitute(ZZZ)))
what_is_my_name(X)
#[1] "X"
what_is_my_name(ZZZ=this)
#[1] "this"
exists("this")
#[1] FALSE
这基本上使用了与Tommy相同的解决方法,但是使用Map(),不需要访问存储列表组件名称的全局变量。
> x <- list(a=11, b=12, c=13)
> Map(function(x, i) paste(i, x), x, names(x))
$a
[1] "a 11"
$b
[1] "b 12"
$c
[1] "c 13
或者,如果你喜欢mapply()
> mapply(function(x, i) paste(i, x), x, names(x))
a b c
"a 11" "b 12" "c 13"
只需编写自己的自定义lapply函数
lapply2 <- function(X, FUN){
if( length(formals(FUN)) == 1 ){
# No index passed - use normal lapply
R = lapply(X, FUN)
}else{
# Index passed
R = lapply(seq_along(X), FUN=function(i){
FUN(X[[i]], i)
})
}
# Set names
names(R) = names(X)
return(R)
}
然后像这样使用:
lapply2(letters, function(x, i) paste(x, i))