代码之家  ›  专栏  ›  技术社区  ›  Mark

如何用dplyr交换datatable中的TRUE和FALSE

  •  0
  • Mark  · 技术社区  · 5 年前

    我希望在a列中的特定行中将值改为它们的相反值(T变为F,反之亦然) datatable

    我知道 x <- !x 为…工作 T/F 变量,但如何完成 dplyr 方法:

    library(dplyr)
    library(datatable)
    library(magrittr)
    mtcars$selected <- T
    mtcars %>% select(selected) %>% slice(c(1,4,5)) %>% mutate(??)
    
    2 回复  |  直到 5 年前
        1
  •  1
  •   r2evans    5 年前

    如果你只是想 子集 那么,@Shree的回答可能是对的。如果您只想反转这些行,但保留所有行,则类似于:

    dplyr :

    library(dplyr)
    mtcars %>%
      mutate(selected = TRUE) %>%
      # the heart of the answer
      mutate(selected = if_else(row_number() %in% c(1, 4, 5), !selected, selected))
    #     mpg cyl  disp  hp drat    wt  qsec vs am gear carb selected
    # 1  21.0   6 160.0 110 3.90 2.620 16.46  0  1    4    4    FALSE
    # 2  21.0   6 160.0 110 3.90 2.875 17.02  0  1    4    4     TRUE
    # 3  22.8   4 108.0  93 3.85 2.320 18.61  1  1    4    1     TRUE
    # 4  21.4   6 258.0 110 3.08 3.215 19.44  1  0    3    1    FALSE
    # 5  18.7   8 360.0 175 3.15 3.440 17.02  0  0    3    2    FALSE
    # 6  18.1   6 225.0 105 2.76 3.460 20.22  1  0    3    1     TRUE
    # 7  14.3   8 360.0 245 3.21 3.570 15.84  0  0    3    4     TRUE
    # 8  24.4   4 146.7  62 3.69 3.190 20.00  1  0    4    2     TRUE
    # ...
    

    你说过 datatable ,我想你是说 data.table ,在这种情况下

    library(data.table)
    DT <- as.data.table(mtcars)
    DT[, selected := TRUE]
    DT[, selected := ifelse(.I %in% c(1, 3, 4), !selected, selected)]
    head(DT, n = 8)
    #      mpg cyl  disp  hp drat    wt  qsec vs am gear carb selected
    #  1: 21.0   6 160.0 110 3.90 2.620 16.46  0  1    4    4    FALSE
    #  2: 21.0   6 160.0 110 3.90 2.875 17.02  0  1    4    4     TRUE
    #  3: 22.8   4 108.0  93 3.85 2.320 18.61  1  1    4    1    FALSE
    #  4: 21.4   6 258.0 110 3.08 3.215 19.44  1  0    3    1    FALSE
    #  5: 18.7   8 360.0 175 3.15 3.440 17.02  0  0    3    2     TRUE
    #  6: 18.1   6 225.0 105 2.76 3.460 20.22  1  0    3    1     TRUE
    #  7: 14.3   8 360.0 245 3.21 3.570 15.84  0  0    3    4     TRUE
    #  8: 24.4   4 146.7  62 3.69 3.190 20.00  1  0    4    2     TRUE
    

    或者像管道一样

    library(magrittr)
    DT <- as.data.table(mtcars)
    DT %>%
      .[, selected := TRUE] %>%
      .[, selected := ifelse(.I %in% c(1, 3, 4), !selected, selected)]
    head(DT, n = 8)
    #     mpg cyl  disp  hp drat    wt  qsec vs am gear carb selected
    # 1: 21.0   6 160.0 110 3.90 2.620 16.46  0  1    4    4    FALSE
    # 2: 21.0   6 160.0 110 3.90 2.875 17.02  0  1    4    4     TRUE
    # 3: 22.8   4 108.0  93 3.85 2.320 18.61  1  1    4    1    FALSE
    # 4: 21.4   6 258.0 110 3.08 3.215 19.44  1  0    3    1    FALSE
    # 5: 18.7   8 360.0 175 3.15 3.440 17.02  0  0    3    2     TRUE
    # 6: 18.1   6 225.0 105 2.76 3.460 20.22  1  0    3    1     TRUE
    # 7: 14.3   8 360.0 245 3.21 3.570 15.84  0  0    3    4     TRUE
    # 8: 24.4   4 146.7  62 3.69 3.190 20.00  1  0    4    2     TRUE
    

    基R ,您可以这样做:

    mtcars$selected <- TRUE
    mtcars$selected[c(1, 3, 4)] <- !mtcars$selected[c(1, 3, 4)]
    head(mtcars, n = 8)
    #                    mpg cyl  disp  hp drat    wt  qsec vs am gear carb selected
    # Mazda RX4         21.0   6 160.0 110 3.90 2.620 16.46  0  1    4    4    FALSE
    # Mazda RX4 Wag     21.0   6 160.0 110 3.90 2.875 17.02  0  1    4    4     TRUE
    # Datsun 710        22.8   4 108.0  93 3.85 2.320 18.61  1  1    4    1    FALSE
    # Hornet 4 Drive    21.4   6 258.0 110 3.08 3.215 19.44  1  0    3    1    FALSE
    # Hornet Sportabout 18.7   8 360.0 175 3.15 3.440 17.02  0  0    3    2     TRUE
    # Valiant           18.1   6 225.0 105 2.76 3.460 20.22  1  0    3    1     TRUE
    # Duster 360        14.3   8 360.0 245 3.21 3.570 15.84  0  0    3    4     TRUE
    # Merc 240D         24.4   4 146.7  62 3.69 3.190 20.00  1  0    4    2     TRUE
    
        2
  •  1
  •   Shree    5 年前

    有一个办法-

    mtcars %>% 
      select(selected) %>% 
      slice(c(1,4,5)) %>% 
      mutate(
        selected = !selected # or as.logical(1 - selected)
      )