创建一个函数,通过将索引值作为 r 中的向量来查找列表中的值
我对 R 还是新手,需要你的帮助。
我有数组形式的数据,我想创建一个适用于数据框的函数,从每行获取 3 个值,并将它们用作索引以从 list 获取值。
该函数应采用三个值来标识数组中的值
select_value <- function(A, B, C) {
result <-array_main[A, B, C]
return (result)
}
,并且该数组为
vector1 <- c(10, 20, 30, 40, 50, 60, 70, 80, 90)
vector2 <- c(100, 200, 300, 400, 500, 600, 700, 800, 900)
vector3 <- c(2, 4, 6, 8, 10, 12, 14, 16, 18)
array_main <- array(c(vector1, vector2, vector3), dim = c(3, 3, 3))
df <- data.frame (C1 = c(1, 2, 3), C2 = c(2, 1, 3), C3 = c(3, 2, 1))
因此该函数应采用 df 中的第一行 [行值为 (1,2,3)] 并使用作为 array_main 的索引来获取 array_main[1,2,3] 并返回值 8,然后 df 的第二行是 (2, 1, 2),所以使用它作为索引数组主
获取 array_main[2, 1, 2] 并返回值 200,最后,第三行为 (3,3,1),因此将其用作 array_main 的索引code> 获取 array_main[3,3,1] 并返回值 90 。
如何获取 (result
) 变量中存储的值作为向量?
非常感谢你的帮助
I am still new to R and need your help.
I have data in the form of an array, and I want to create a function that applies to a dataframe, to get 3 values from each row and use them as indices to get values from the list .
The function should take three values to identify the value in the array
select_value <- function(A, B, C) {
result <-array_main[A, B, C]
return (result)
}
and the array is
vector1 <- c(10, 20, 30, 40, 50, 60, 70, 80, 90)
vector2 <- c(100, 200, 300, 400, 500, 600, 700, 800, 900)
vector3 <- c(2, 4, 6, 8, 10, 12, 14, 16, 18)
array_main <- array(c(vector1, vector2, vector3), dim = c(3, 3, 3))
df <- data.frame (C1 = c(1, 2, 3), C2 = c(2, 1, 3), C3 = c(3, 2, 1))
So the function should take for example the first row from df
[row values are (1,2,3)] and use that as indices for array_main to get array_main[1,2,3]
and return the value 8, then the second row from df which is (2, 1, 2), so use that as indices for array_main
to get array_main[2, 1, 2]
and return the value 200, finally, the third row which is (3,3,1), so use that as indices for array_main
to get array_main[3,3,1]
and return the value 90 .
How can I get the values stored in the (result
) variable as a vector?
Thank you so much for your help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您实际上根本不需要
select_value
函数。您可以像这样完成整个事情:学习要点
还有一些需要注意的点是您的函数
select_value
比它需要的要长。编写:与编写相同
第二点需要注意的是,编写一个依赖于函数外部的数据而不是作为参数传入的函数并不是一个好主意。编写此类函数的更好方法是:
但是,当我们这样做时,我们意识到
select_value
本质上与方括号本身是相同的函数。上面的函数几乎与将select_value
定义为相同:也许更有用的是像这样定义你的函数:
在你的情况下,它会产生:
You don't really need the
select_value
function at all. You can do the entire thing like this:Learning points
A couple of other points to note are than your function
select_value
is longer than it needs to be. Writing:Is the same as writing
The second point to note is that it isn't a great idea to write a function that relies on data from outside the function which isn't being passed in as an argument. A better way to write such a function would be:
However, when we do that, we realise that
select_value
is essentially the same function as the square bracket itself. The above function is almost identical to definingselect_value
as:Perhaps more useful would be defining your function like this:
Which, in your case, would produce: