r 中的数字列运算与另一个因子级别的条件

问题描述 投票:0回答:2

给定数据集

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))

我想根据因子

respiration_net
的级别将数字列
status
中的每个值除以100或50。也就是说,如果
status
healthy
,我将按 100 进行划分,如果是
rotten
,我将按 50 进行划分。到目前为止,我可以简单地划分列,但无法嵌套与因素水平。 有什么帮助吗?预先感谢;)

r if-statement numeric division
2个回答
0
投票

你听说过合并吗?

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

0
投票

您可以使用

if_else
case_when
case_match
,如下所示,根据
respiration_net
有条件地划分
status

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
© www.soinside.com 2019 - 2024. All rights reserved.