R语言 根据条件更改单元格中的值

ar5n3qh5  于 2023-03-27  发布在  其他
关注(0)|答案(3)|浏览(155)

我有一个 Dataframe ,看起来像这样:

> dput(dfold)
structure(list(Person_id = c(50L, 234L, 555L), Days = c(94L, 
102L, 50L), Outocme = c(1L, 1L, 0L)), class = "data.frame", row.names = c(NA, 
-3L))

基本上,如果Days列超过100,我希望将值更改为100。相应的Outcome应等于0。
另一方面,如果结果已经为0(Days列是否超过100天并不重要),则Days列应该正好等于100。
下面是新 Dataframe 的外观:

> dput(dfnew)
structure(list(Person_id = c(50L, 234L, 555L), Days = c(94L, 
100L, 100L), Outocme = c(1L, 0L, 0L)), class = "data.frame", row.names = c(NA, 
-3L))
rbl8hiat

rbl8hiat1#

下面是一个使用Tidyverse语法的解决方案:

library(tidyverse)

df <- tribble(
  ~Person_id, ~Days, ~Outcome,
  50L,   94L,       1L,
  234L,  102L,       1L,
  555L,   50L,       0L
)

df |> 
  mutate(
    Days = if_else(Outcome == 0L, 100, Days),
    Outcome = if_else(Days > 100, 0, Outcome),
    Days = if_else(Days > 100, 100, Days)
  )
#> # A tibble: 3 × 3
#>   Person_id  Days Outcome
#>       <int> <dbl>   <dbl>
#> 1        50    94       1
#> 2       234   100       0
#> 3       555   100       0

创建于2023-03-23带有reprex v2.0.2

wrrgggsh

wrrgggsh2#

简单的事情怎么样,分两步来做?

data <- structure(list(Person_id = c(50L, 234L, 555L), 
                       Days = c(934L, 102L, 50L), 
                       Outcome = c(1L, 1L, 0L)), 
                  class = "data.frame", row.names = c(NA, -3L))

data$Days[data$Days > 100] <- 100
data$Outcome[data$Days >= 100] <- 0
print(data)
ct2axkht

ct2axkht3#

使用case_when的方法

library(dplyr)

dfold %>% 
  mutate(Days = case_when(Days > 100 ~ 100, 
                          Days < 100 & Outocme == 0 ~ 100, 
                          Days < 100 & Outocme == 1 ~ Days))
  Person_id Days Outocme
1        50   94       1
2       234  100       1
3       555  100       0

相关问题