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

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

问题描述

我在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的每个元素,向量都会重新调整x - K0,以较大者为准.如果需要此功能,请使用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天全站免登陆