【问题标题】:Change value in columns based off of another column in dataframe R根据数据框 R 中的另一列更改列中的值
【发布时间】:2021-11-24 09:56:39
【问题描述】:
    df ex:
# A tibble: 20 x 2
   Autoswing `Duration(seconds)`
   <chr>                   <dbl>
 1 ✗                           4
 2 ✗                           5
 3 ✗                           7
 4 ✗                           6
 5 ✗                           8
 6 ✗                           9
 7 ✗                          11
 8 ✗                          16
 9 ✗                          16
10 ✗                           8
11 ✗                          12
12 ✗                          15
13 ✗                          10
14 ✗                         Inf
15 ✗                          14
16 ✗                         Inf
17 ✗                         Inf
18 ✗                         Inf
19 ✗                         Inf
20 ✗                         Inf

如果 Duration(seconds) 为 inf,我想在 Autoswing 中将所有 ✗ 更改为 ✓。我试图使用 if 语句

【问题讨论】:

  • 与dput分享您的数据

标签: r dataframe if-statement


【解决方案1】:

我们可以使用base R 方法,即使用is.infinite 检查无限值来创建逻辑向量并进行赋值

df$Autoswing[is.infinite(df$`Duration(seconds)`)] <- "✓"

-输出

> df
# A tibble: 20 × 2
   Autoswing `Duration(seconds)`
   <chr>                   <dbl>
 1 ✗                           4
 2 ✗                           5
 3 ✗                           7
 4 ✗                           6
 5 ✗                           8
 6 ✗                           9
 7 ✗                          11
 8 ✗                          16
 9 ✗                          16
10 ✗                           8
11 ✗                          12
12 ✗                          15
13 ✗                          10
14 ✓                         Inf
15 ✗                          14
16 ✓                         Inf
17 ✓                         Inf
18 ✓                         Inf
19 ✓                         Inf
20 ✓                         Inf

或者data.table

library(data.table)
setDT(df)[is.infinite(`Duration(seconds)`), Autoswing := "✓"]

或与dplyr 一起使用replace

library(dplyr)
df %>% 
   mutate(Autoswing = replace(Autoswing, is.infinite(`Duration(seconds)`), "✓"))
# A tibble: 20 × 2
   Autoswing `Duration(seconds)`
   <chr>                   <dbl>
 1 ✗                           4
 2 ✗                           5
 3 ✗                           7
 4 ✗                           6
 5 ✗                           8
 6 ✗                           9
 7 ✗                          11
 8 ✗                          16
 9 ✗                          16
10 ✗                           8
11 ✗                          12
12 ✗                          15
13 ✗                          10
14 ✓                         Inf
15 ✗                          14
16 ✓                         Inf
17 ✓                         Inf
18 ✓                         Inf
19 ✓                         Inf
20 ✓                         Inf

数据

df <- structure(list(Autoswing = c("✗", "✗", "✗", "✗", "✗", 
"✗", "✗", "✗", "✗", "✗", "✗", "✗", "✗", "✗", 
"✗", "✗", "✗", "✗", "✗", "✗"), `Duration(seconds)` = c(4, 
5, 7, 6, 8, 9, 11, 16, 16, 8, 12, 15, 10, Inf, 14, Inf, Inf, 
Inf, Inf, Inf)), class = c("tbl_df", "tbl", "data.frame"), 
row.names = c(NA, 
-20L))

【讨论】:

    【解决方案2】:

    另一个基本 R 选项是使用 transform:

    df <- transform(df, Autoswing = ifelse(`Duration(seconds)` == "Inf", "✓", Autoswing))
    

    输出:

       Autoswing Duration.seconds.
    1          ✗                 4
    2          ✗                 5
    3          ✗                 7
    4          ✗                 6
    5          ✗                 8
    6          ✗                 9
    7          ✗                11
    8          ✗                16
    9          ✗                16
    10         ✗                 8
    11         ✗                12
    12         ✗                15
    13         ✗                10
    14         ✓               Inf
    15         ✗                14
    16         ✓               Inf
    17         ✓               Inf
    18         ✓               Inf
    19         ✓               Inf
    20         ✓               Inf
    

    【讨论】:

      【解决方案3】:
      library(dplyr)
      
      df <-
        tibble(
          Autoswing = c("✗","✗"),
          `Duration(seconds)` = c(2,Inf)
        )
      
      df %>% 
        mutate(Autoswing = if_else(`Duration(seconds)` == Inf,"✓",Autoswing)) %>% 
        glimpse()
      
      Rows: 2
      Columns: 2
      $ Autoswing           <chr> "✗", "✓"
      $ `Duration(seconds)` <dbl> 2, Inf
      

      【讨论】:

        【解决方案4】:
        library(tidyverse)
        
        df <- tibble(
          Autoswing = c("✗", "✗"),
          Duration = c(5, Inf)
        )
        df
        #> # A tibble: 2 × 2
        #>   Autoswing Duration
        #>   <chr>        <dbl>
        #> 1 ✗                5
        #> 2 ✗              Inf
        df %>%
          mutate(
            Autoswing = Autoswing %>% map2_chr(Duration, ~ ifelse(is.infinite(.y), "✓", .x))
          )
        #> # A tibble: 2 × 2
        #>   Autoswing Duration
        #>   <chr>        <dbl>
        #> 1 ✗                5
        #> 2 ✓              Inf
        

        由reprex package 创建于 2021-10-03 (v2.0.1)

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2017-11-11
          • 1970-01-01
          • 2018-04-15
          • 2019-03-30
          • 1970-01-01
          • 1970-01-01
          • 2021-09-05
          相关资源
          最近更新 更多