创建一个函数,通过将索引值作为 r 中的向量来查找列表中的值

发布于 2025-01-15 14:25:07 字数 885 浏览 1 评论 0原文

我对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倚栏听风 2025-01-22 14:25:07

您实际上根本不需要 select_value 函数。您可以像这样完成整个事情:

array_main[as.matrix(df)]
#> [1]   8 200  90

学习要点

还有一些需要注意的点是您的函数select_value比它需要的要长。编写:

select_value <- function(A, B, C) {
  result <-array_main[A, B, C]
  return (result)
}

与编写相同

select_value <- function(A, B, C) {
  array_main[A, B, C]
}

第二点需要注意的是,编写一个依赖于函数外部的数据而不是作为参数传入的函数并不是一个好主意。编写此类函数的更好方法是:

select_value <- function(data, A, B, C) {
  data[A, B, C]
}

但是,当我们这样做时,我们意识到 select_value 本质上与方括号本身是相同的函数。上面的函数几乎与将 select_value 定义为相同:

select_value <- `[`

也许更有用的是像这样定义你的函数:

select_values <- function(data_array, index_df) {
  data_array[as.matrix(index_df)]
}

在你的情况下,它会产生:

select_values(array_main, df)
#> [1]   8 200  90

You don't really need the select_value function at all. You can do the entire thing like this:

array_main[as.matrix(df)]
#> [1]   8 200  90

Learning points

A couple of other points to note are than your function select_value is longer than it needs to be. Writing:

select_value <- function(A, B, C) {
  result <-array_main[A, B, C]
  return (result)
}

Is the same as writing

select_value <- function(A, B, C) {
  array_main[A, B, C]
}

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:

select_value <- function(data, A, B, C) {
  data[A, B, C]
}

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 defining select_value as:

select_value <- `[`

Perhaps more useful would be defining your function like this:

select_values <- function(data_array, index_df) {
  data_array[as.matrix(index_df)]
}

Which, in your case, would produce:

select_values(array_main, df)
#> [1]   8 200  90
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文