如何根据每行的3个观察结果返回真或错误?

发布于 2025-02-13 16:30:04 字数 1153 浏览 0 评论 0原文

看起来像这样

ID抗原浓度
C001A10.291259335
C001A20.580050639
C001A30.002235505
C001A40.848792002
C002C002
数据C002
有一个A1C002
0.194151877
C003A10.0142237
C003A2 A20.027409926
C003A3 A3 A30.000117995
C003A40.042202259

每个参与者具有4个与IT相关的抗原,并且每个抗原的平均浓度在每个抗原中都在每个特定的范围内,这是不同的,这是每个抗原的范围都在每个范围内,这是属于每个抗原的量。

我正在尝试创建一个函数,其中每个参与者 - 抗原浓度组合都会检查,并在新列中返回一个真/错误。例如,如果参与者= C001,抗原= A1,浓度在0.77至1.43之间,则返回true。如果对于参与者和抗原的任何组合,浓度不超出范围,请返回错误。

我尝试使用循环,如果其他功能和其他效率低下的方式,但效果不足。我认为我不会尽我所能地接近这一点,因此,如果有人可以将我指向任何有用的功能或文档的方向,我将非常感激。

先感谢您!

I have a dataset that looks like this:

IDantigenconcentration
C001a10.291259335
C001a20.580050639
C001a30.002235505
C001a40.848792002
C002a10.066808174
C002a20.130661773
C002a30.000530089
C002a40.194151877
C003a10.0142237
C003a20.027409926
C003a30.000117995
C003a40.042202259

Each participant has 4 antigens associated with it, and the mean concentration for each antigen has to fall within a specific range, which is different for each antigen.

I'm trying to create a function where each participant-antigen-concentration combination is checked and a TRUE/FALSE is returned in a new column. For example if Participant = C001, antigen = a1, and Concentration is between 0.77 and 1.43, return TRUE. If for any combination of participant and antigen the concentration is out of range, return FALSE.

I've tried using for loops, if else functions and other inefficient ways but have come up short. I don't think I'm approaching this in as logical way as I should so if anyone could point me in the direction of any useful functions or documentation for this kind of problem I'd be very grateful.

Thank you in advance!

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

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

发布评论

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

评论(1

恰似旧人归 2025-02-20 16:30:04

您可以使用case_when

df <- tibble(ID = c("C001", "C001", "C002", "C002"),
             antigen = c("a1", "a2", "a1", "a2"),
             concentration = c(0.291259335, 0.580050639, 0.066808174, 0.130661773))
      
library(tidyverse)
       
df |>
  mutate(within_range = case_when(antigen == "a1" & between(concentration, 0.77, 1.43) ~ TRUE,
                                  antigen == "a2" & between(concentration, 0.1, 0.5) ~ TRUE,
                                  TRUE ~ FALSE)
         )

输出:

# A tibble: 4 × 4
  ID    antigen concentration within_range
  <chr> <chr>           <dbl> <lgl>       
1 C001  a1             0.291  FALSE       
2 C001  a2             0.580  FALSE       
3 C002  a1             0.0668 FALSE       
4 C002  a2             0.131  TRUE        

You could use case_when:

df <- tibble(ID = c("C001", "C001", "C002", "C002"),
             antigen = c("a1", "a2", "a1", "a2"),
             concentration = c(0.291259335, 0.580050639, 0.066808174, 0.130661773))
      
library(tidyverse)
       
df |>
  mutate(within_range = case_when(antigen == "a1" & between(concentration, 0.77, 1.43) ~ TRUE,
                                  antigen == "a2" & between(concentration, 0.1, 0.5) ~ TRUE,
                                  TRUE ~ FALSE)
         )

Output:

# A tibble: 4 × 4
  ID    antigen concentration within_range
  <chr> <chr>           <dbl> <lgl>       
1 C001  a1             0.291  FALSE       
2 C001  a2             0.580  FALSE       
3 C002  a1             0.0668 FALSE       
4 C002  a2             0.131  TRUE        
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文