R 中的数值列运算,条件来自另一个因子水平

numeric column operation in r with condition from another factor level

提问人:Rudy Benetti 提问时间:9/18/2023 更新时间:9/18/2023 访问量:38

问:

给定数据集df

df<-structure(list(status = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), levels = c("healthy", 
"rotten", "control"), class = "factor"), respiration_net = c(3.0644, 
0.331, -1.9911, 5.4395, 3.1105, 1.4436, 1.3818, 0.7699, 0.0725, 
-6.57635, -6.59815, -6.52, -6.019, -6.3689, -6.0336, -4.367, 
-4.1033, -3.8167, -2.5337, -2.2272, -2.1751)), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -21L), groups = structure(list(
    status = structure(1:2, levels = c("healthy", "rotten", "control"
    ), class = "factor"), .rows = structure(list(1:9, 10:21), ptype = integer(0), class = c("vctrs_list_of", 
    "vctrs_vctr", "list"))), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -2L), .drop = TRUE))

我想根据因子的水平将数值列中的每个值除以 100 或 50。也就是说,如果是,我将每 100 除以,如果是,我将每 50 除法。到目前为止,我能够简单地划分列,但我无法嵌套与因子水平相关的条件。 有什么帮助吗?提前致谢 ;)respiration_netstatusstatushealthyrotten

R IF-语句 数值 除法

评论


答:

0赞 user2974951 9/18/2023 #1

你听说过合并吗?

df2=data.frame(
  "status"=c("healthy","rotten"),
  "divide"=c(100,50)
)
merge(df,df2,by="status")

    status respiration_net divide
1  healthy         3.06440    100
2  healthy         0.33100    100
3  healthy        -1.99110    100
4  healthy         5.43950    100
5  healthy         3.11050    100
6  healthy         1.44360    100
7  healthy         1.38180    100
8  healthy         0.76990    100
9  healthy         0.07250    100
10  rotten        -6.57635     50
11  rotten        -6.59815     50
12  rotten        -6.52000     50
13  rotten        -6.01900     50
14  rotten        -6.36890     50
15  rotten        -6.03360     50
16  rotten        -4.36700     50
17  rotten        -4.10330     50
18  rotten        -3.81670     50
19  rotten        -2.53370     50
20  rotten        -2.22720     50
21  rotten        -2.17510     50
1赞 stefan 9/18/2023 #2

你可以像下面那样使用 or or 来有条件地除以:if_elsecase_whencase_matchrespiration_netstatus

library(dplyr, warn=FALSE)

df |> 
  ungroup() |> 
  mutate(respiration_net = case_match(
    status,
    "healthy" ~ respiration_net / 100,
    "rotten" ~ respiration_net / 50,
    .default = respiration_net
  ))
#> # A tibble: 21 × 2
#>    status  respiration_net
#>    <fct>             <dbl>
#>  1 healthy        0.0306  
#>  2 healthy        0.00331 
#>  3 healthy       -0.0199  
#>  4 healthy        0.0544  
#>  5 healthy        0.0311  
#>  6 healthy        0.0144  
#>  7 healthy        0.0138  
#>  8 healthy        0.00770 
#>  9 healthy        0.000725
#> 10 rotten        -0.132   
#> # ℹ 11 more rows

评论

0赞 Rudy Benetti 10/1/2023
谢谢 Stefan 的回答,只是在我的控制台中,虽然我加载了它没有显示该功能。但是我成功地解决了你的建议.library(dplyr, warn=FALSE)case_matchcase_when
1赞 stefan 10/6/2023
尝试更新 . 仅在 dplyr >= 1.1.0 中可用。dplyrcase_match
1赞 Rudy Benetti 10/17/2023
是的,这只是一个更新的问题,谢谢 Stefan :)