适用于向量列表 [英] Apply to list of vectors

查看:27
本文介绍了适用于向量列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 R 中的数值向量列表(每个向量的大小不同).假设该列表有 10 个元素.

I have list of numerical vectors (with different sizes each) in R. Let's say the list has 10 elements.

我想对每个向量的每个元素应用一个函数(类似于 max(x - K, 0) 其中 K 是一个固定常数)列表,并获得具有完全相同结构的输出(因此是向量列表).

I would like to apply a function (something like max(x - K, 0) where K is a fixed constant) to each elements of each vector of the list, and get an output with exactly the same structure (thus a list of vectors).

我尝试了 sapply(data, function(x) max(x-K, 0)) 但我得到了一个大小为 10 的向量.顺便说一下,我不知道它的组件是什么.我也直接尝试了 max(data-K,0) ,但我收到一条错误消息.

I tried sapply(data, function(x) max(x-K, 0)) but I get a vector of size 10. I don't know exactly what are its components by the way. I also tried max(data-K,0) directly, but I get an error message.

谁能帮帮我?

推荐答案

## dummy data
set.seed(1)
ll <- as.list(data.frame(matrix(rnorm(10 * 10), ncol = 10)))

R> str(ll)
List of 10
 $ X1 : num [1:10] -0.626 0.184 -0.836 1.595 0.33 ...
 $ X2 : num [1:10] 1.512 0.39 -0.621 -2.215 1.125 ...
 $ X3 : num [1:10] 0.919 0.7821 0.0746 -1.9894 0.6198 ...
 $ X4 : num [1:10] 1.3587 -0.1028 0.3877 -0.0538 -1.3771 ...
 $ X5 : num [1:10] -0.165 -0.253 0.697 0.557 -0.689 ...
 $ X6 : num [1:10] 0.398 -0.612 0.341 -1.129 1.433 ...
 $ X7 : num [1:10] 2.4016 -0.0392 0.6897 0.028 -0.7433 ...
 $ X8 : num [1:10] 0.476 -0.71 0.611 -0.934 -1.254 ...
 $ X9 : num [1:10] -0.569 -0.135 1.178 -1.524 0.594 ...
 $ X10: num [1:10] -0.543 1.208 1.16 0.7 1.587 ...

有了这个,如果你想要一个原子向量输出,你所拥有的就是正确的.要查看关系,请查看向量元素的名称

With this, what you had is correct if you want an atomic vector output. To see the relationship look at the names of the vector elements

R> ( out <- sapply(ll, function(x, K) max(x - K, 0), K = 0.3) )
    X1     X2     X3     X4     X5     X6     X7     X8     X9    X10 
1.2953 1.2118 0.6190 1.0587 0.5811 1.6804 2.1016 0.3107 0.8781 1.2868

它们的顺序与列表元素的名称相同:

they are in the same order as the names of the list elements:

R> names(out)
 [1] "X1"  "X2"  "X3"  "X4"  "X5"  "X6"  "X7"  "X8"  "X9"  "X10"
R> names(ll)
 [1] "X1"  "X2"  "X3"  "X4"  "X5"  "X6"  "X7"  "X8"  "X9"  "X10"
R> all.equal(names(out), names(ll))
[1] TRUE

如果你想要一些看起来像原始列表的东西,那么使用 lapply() 代替

If you want something back that looks like the original list, then use lapply() instead

R> lapply(ll, function(x, K) max(x - K, 0), K = 0.3)
$X1
[1] 1.295

$X2
[1] 1.212

$X3
[1] 0.619

$X4
[1] 1.059

$X5
[1] 0.5811
....

然而,不清楚你想要最大值还是并行最大值,即对于每个列表元素,一个向量重新调整 x - K0,以较大者为准, 对于 x 的每个元素.如果你想要这个,这个功能在 pmax() 函数中:

However, it is not clear that you want the maximum or the parallel maximum, i.e. for each list element a vector retuning x - K or 0, whichever is larger, for each element of x. If you want this, that functionality is in the pmax() function:

R> lapply(ll, function(x, K) pmax(x - K, 0), K = 0.3)
$X1
 [1] 0.00000 0.00000 0.00000 1.29528 0.02951 0.00000 0.18743 0.43832 0.27578
[10] 0.00000

$X2
 [1] 1.21178 0.08984 0.00000 0.00000 0.82493 0.00000 0.00000 0.64384 0.52122
[10] 0.29390

$X3
 [1] 0.6190 0.4821 0.0000 0.0000 0.3198 0.0000 0.0000 0.0000 0.0000 0.1179

$X4
 [1] 1.05868 0.00000 0.08767 0.00000 0.00000 0.00000 0.00000 0.00000 0.80003
[10] 0.46318
....

或通过 sapply,将结果简化为矩阵:

or via sapply, which simplifies the reuslt to a matrix:

R> sapply(ll, function(x, K) pmax(x - K, 0), K = 0.3)
           X1      X2     X3      X4      X5      X6     X7     X8      X9
 [1,] 0.00000 1.21178 0.6190 1.05868 0.00000 0.09811 2.1016 0.1755 0.00000
 [2,] 0.00000 0.08984 0.4821 0.00000 0.00000 0.00000 0.0000 0.0000 0.00000
 [3,] 0.00000 0.00000 0.0000 0.08767 0.39696 0.04112 0.3897 0.3107 0.87809
 [4,] 1.29528 0.00000 0.0000 0.00000 0.25666 0.00000 0.0000 0.0000 0.00000
 [5,] 0.02951 0.82493 0.3198 0.00000 0.00000 1.13302 0.0000 0.0000 0.29395
 [6,] 0.00000 0.00000 0.0000 0.00000 0.00000 1.68040 0.0000 0.0000 0.03295
 [7,] 0.18743 0.00000 0.0000 0.00000 0.06458 0.00000 0.0000 0.0000 0.76310
 [8,] 0.43832 0.64384 0.0000 0.00000 0.46853 0.00000 1.1656 0.0000 0.00000
 [9,] 0.27578 0.52122 0.0000 0.80003 0.00000 0.26972 0.0000 0.0000 0.07002
[10,] 0.00000 0.29390 0.1179 0.46318 0.58111 0.00000 1.8726 0.0000 0.00000
         X10
 [1,] 0.0000
 [2,] 0.9079
 [3,] 0.8604
 [4,] 0.4002
 [5,] 1.2868
 [6,] 0.2585
 [7,] 0.0000
 [8,] 0.0000
 [9,] 0.0000
[10,] 0.0000

最后,请注意将运行所需的所有信息传递给函数是一种很好的做法.您依赖 R 找到 K 某处.最好让您的匿名函数接受一个参数 K 并传递您在调用该函数时希望使用的 K 的值.这就是我在上面的示例中所做的,例如

Finally, note that it is good practice to pass into your function all information it needs to run. You are relying on R finding K somewhere. It is much better to have your anonymous function take an argument K and pass along the value of K you wish to use when you call the function. This is what I did above in my examples, e.g.

lapply(ll, function(x, K) pmax(x - K, 0), K = 0.3)
           |___________ FUN ___________| |_ ARGS_|

我用 ARGS 标记的位是您提供参数 FUN 的函数所需的任何进一步参数的地方.

where the bit I have marked with ARGS is where you supply any further arguments required by the function supplied to argument FUN.

这篇关于适用于向量列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆