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

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

How to Subtract Values Within Two Lists

问题

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

# 创建一个用于存储坐标差异的列表
coord.diff <- list()

# 循环遍历数据框的每个元素
for(i in 1:length(df)){
  coord.diff[[i]] <- (list2[[i]] - list1[[i]])
}

这段代码会计算两个列表中相应元素之间的差异,并将结果存储在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):

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

which returns the following errors:

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

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

&#39;&#39;&#39;r
list2[[1]]-list1[[1]]
&#39;&#39;&#39;r

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

答案1

得分: 0

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

# 设置虚拟数据
list1 <- list(TM1 = c(300, 350, 200),
              TM2 = c(175, 150))

list2 <- list(TM1 = c(315, 345, 205),
              TM2 = c(170, 165))

# 应用map2函数定义列表输入并执行操作(y = list2,x = list1)
purrr::map2(list1, list2, ~.y - .x)

$TM1
[1] 15 -5  5

$TM2
[1] -5 15

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

myfun <- function(x, y){
    ret <- y - x
    return(ret)
}

purrr::map2(list1, list2, ~ myfun(.x, .y))

$TM1
[1] 15 -5  5

$TM2
[1] -5 15

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

mapply(function(x ,y) {
    ret <- y - x
    return(ret)
}, 
x = list1, y = list2)

$TM1
[1] 15 -5  5

$TM2
[1] -5 15

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

Map(`-`, list1, list2)

$TM1
[1] -15   5  -5

$TM2
[1]   5 -15

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

purrr::map2(list2, list1, `-`)

$TM1
[1] 15 -5  5

$TM2
[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:

# set up dummy data
list1 &lt;- list(TM1 = c(300, 350, 200),
              TM2 = c(175,150))

list2 &lt;- list(TM1 = c(315, 345, 205),
              TM2 = c(170,165))

# apply map2 function defining list inputs and performin the operation 
# (y = list2, x = list1)
purrr::map2(list1, list2, ~.y - .x)

$TM1
[1] 15 -5  5

$TM2
[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:

myfun &lt;- function(x, y){
    ret &lt;- y - x
    return(ret)
}

purrr::map2(list1, list2, ~ myfun(.x, .y))

$TM1
[1] 15 -5  5

$TM2
[1] -5 15

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

mapply(function(x ,y) {
    ret &lt;- y - x
    return(ret)
}, 
x = list1, y = list2)

$TM1
[1] 15 -5  5

$TM2
[1] -5 15

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

Map(`-`, list1, list2)

$TM1
[1] -15   5  -5

$TM2
[1]   5 -15

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

purrr::map2(list2, list1, `-`)

$TM1
[1] 15 -5  5

$TM2
[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:

确定