如何在两个列表中进行数值相减。

huangapple go评论99阅读模式
英文:

How to Subtract Values Within Two Lists

问题

以下是你要的代码翻译部分:

  1. # 创建一个用于存储坐标差异的列表
  2. coord.diff <- list()
  3. # 循环遍历数据框的每个元素
  4. for(i in 1:length(df)){
  5. coord.diff[[i]] <- (list2[[i]] - list1[[i]])
  6. }

这段代码会计算两个列表中相应元素之间的差异,并将结果存储在coord.diff列表中。如果你有任何其他问题,或需要进一步的帮助,请告诉我。

英文:

I have two lists of animal tracking data. Each list contains the animal ID and a list of utm coordinates. My lists look something like this:

list1

list1 List[2] List of length 2
TM1 integer[3] 300,350,200
TM2 integer[2] 175,150

list2

list2 List[2] List of length 2
TM1 double[3] 315,345,205
TM2 double[2] 170,165

What I would like to do is subtract the coordinates for each individual to get a final list that looks something like this:

X List[2] List of length 2
TM1 integer[3] 15,-5,5
TM2 integer[2] -5,15

I tried setting up a for loop to subtract the values (just a note, df here is a placeholder for the original dataframe of length 2):

  1. &#39;&#39;&#39;r
  2. coord.diff&lt;-list()
  3. for(i in 1:length(df)){
  4. coord.diff[i]&lt;-(list2[[i]]-list1[[i]])
  5. }
  6. &#39;&#39;&#39;r

which returns the following errors:

  1. &#39;&#39;&#39;r
  2. 1: In coord.diff[i] &lt;- (list2[[i]] - list1[[i]]) :
  3. number of items to replace is not a multiple of replacement length
  4. 2: In coord.diff[i] &lt;- (list2[[i]] - list1[[i]]) :
  5. number of items to replace is not a multiple of replacement length
  6. &#39;&#39;&#39;r

Oddly enough however, when I do the following I actually get the list of differences that I want:

  1. &#39;&#39;&#39;r
  2. list2[[1]]-list1[[1]]
  3. &#39;&#39;&#39;r

but I would like to be able to automate the process through a loop.

答案1

得分: 0

tidyverse中的一个快速解决方案是使用purrr包,更具体地说是purrr::map2函数,该函数适用于两个输入:

  1. # 设置虚拟数据
  2. list1 <- list(TM1 = c(300, 350, 200),
  3. TM2 = c(175, 150))
  4. list2 <- list(TM1 = c(315, 345, 205),
  5. TM2 = c(170, 165))
  6. # 应用map2函数定义列表输入并执行操作(y = list2,x = list1)
  7. purrr::map2(list1, list2, ~.y - .x)
  8. $TM1
  9. [1] 15 -5 5
  10. $TM2
  11. [1] -5 15

为了更好地理解,如果要应用的数学操作更复杂,即您可以单独定义要应用的函数并通过purrr::map2调用应用它:

  1. myfun <- function(x, y){
  2. ret <- y - x
  3. return(ret)
  4. }
  5. purrr::map2(list1, list2, ~ myfun(.x, .y))
  6. $TM1
  7. [1] 15 -5 5
  8. $TM2
  9. [1] -5 15

基本的R选项看起来像这样,使用mapply

  1. mapply(function(x ,y) {
  2. ret <- y - x
  3. return(ret)
  4. },
  5. x = list1, y = list2)
  6. $TM1
  7. [1] 15 -5 5
  8. $TM2
  9. [1] -5 15

正如@onyambu在评论中指出的那样,这是简洁的基本R方法:

  1. Map(`-`, list1, list2)
  2. $TM1
  3. [1] -15 5 -5
  4. $TM2
  5. [1] 5 -15

这种简化也可以与purrr::map2()调用一起使用,正如@onyambu在评论中也指出的那样:

  1. purrr::map2(list2, list1, `-`)
  2. $TM1
  3. [1] 15 -5 5
  4. $TM2
  5. [1] -5 15
英文:

One fast solution within the tidyverse is to use the purrr package, more specifically the purrr::map2 function, which works on two inputs:

  1. # set up dummy data
  2. list1 &lt;- list(TM1 = c(300, 350, 200),
  3. TM2 = c(175,150))
  4. list2 &lt;- list(TM1 = c(315, 345, 205),
  5. TM2 = c(170,165))
  6. # apply map2 function defining list inputs and performin the operation
  7. # (y = list2, x = list1)
  8. purrr::map2(list1, list2, ~.y - .x)
  9. $TM1
  10. [1] 15 -5 5
  11. $TM2
  12. [1] -5 15

For better comprehention, if the mathematical operation to apply has more complexity i.e., you can define the function to apply seperately and apply it via the purrr::map2 call:

  1. myfun &lt;- function(x, y){
  2. ret &lt;- y - x
  3. return(ret)
  4. }
  5. purrr::map2(list1, list2, ~ myfun(.x, .y))
  6. $TM1
  7. [1] 15 -5 5
  8. $TM2
  9. [1] -5 15

The base R option would look something like this, using mapply:

  1. mapply(function(x ,y) {
  2. ret &lt;- y - x
  3. return(ret)
  4. },
  5. x = list1, y = list2)
  6. $TM1
  7. [1] 15 -5 5
  8. $TM2
  9. [1] -5 15

as @onyambu pointed out in the comment this is consice base are approach:

  1. Map(`-`, list1, list2)
  2. $TM1
  3. [1] -15 5 -5
  4. $TM2
  5. [1] 5 -15

This simplification can also be used with the purrr::map2() call as @onyambu pointed out also in the comments:

  1. purrr::map2(list2, list1, `-`)
  2. $TM1
  3. [1] 15 -5 5
  4. $TM2
  5. [1] -5 15

huangapple
  • 本文由 发表于 2023年2月27日 01:06:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/75573640.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定