如何在每个单个单元格中制作r方程

发布于 2025-02-03 02:32:57 字数 407 浏览 5 评论 0原文

我有2列不同的尺寸A和B。A

| B

5 | 4
1 | 2
3 |

我怎么说如果Cella为<然后,Cellb给我Cellb-cella?,因此它计算出:

“ 5(a1)大于4(b1)和2(b2)”,因此没有结果

” 1(A2)小于4(b1)和2(b2)“所以我们有3和1

“ 3(A3)小于4(b1),大于2(b2)”,所以我们有1

结果=(3,1,1)

我最接近的是: 如果(a< b){b- a}

但这仅与相同大​​小的列一起使用,我希望列A的每个单独的单独的单独的单独的单独的单独的单独的单元与B列的每个单独的单元相互作用。那会吗?

I have 2 columns of different size, A and B.

A | B

5 | 4
1 | 2
3 |

How do I say If CellA is < CellB then give me CellB-CellA? So it calculates:

"5(A1) is bigger than 4(B1) and 2(B2)" so no results

"1(A2) is smaller than 4(B1) and 2(B2)" so we have 3 and 1

"3(A3) is smaller than 4(B1) and bigger than 2(B2)" so we have 1

Result = (3, 1, 1)

The closest I got was:
if (A < B) {B - A}

But that only works with columns of same size, I want each individual cell of Column A to interact with each individual cell of column B. How can I do that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

阳光下的泡沫是彩色的 2025-02-10 02:32:57

由于不同尺寸的列必须是列表而不是data.frame。因此,解决方案:

listAB <- list(A = c(5,1,3) , B = c(4,2))

equ <- function(li){
  result <- vector("numeric")
  for (x in li$A){
    result <- append(result , sapply(li$B , function(y) if(x < y) y - x))
  }
  unlist(result)
}

equ(listAB)
#> [1] 3 1 1

在2022-05-29上由 reprex软件包(v2.0.1)

Since the columns of different sizes then it must be list and not data.frame so the solution :

listAB <- list(A = c(5,1,3) , B = c(4,2))

equ <- function(li){
  result <- vector("numeric")
  for (x in li$A){
    result <- append(result , sapply(li$B , function(y) if(x < y) y - x))
  }
  unlist(result)
}

equ(listAB)
#> [1] 3 1 1

Created on 2022-05-29 by the reprex package (v2.0.1)

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