Mark
Mark

Reputation: 2899

How to swap TRUE and FALSE in a dataframe with dplyr

I'm looking to change values into their opposite (T becomes F, and vice versa) in specific rows in a column in a datatable

I know that x <- !x works for T/F variables but how to finish this dplyr approach:

library(dplyr)
library(datatable)
library(magrittr)
mtcars$selected <- T
mtcars %>% select(selected) %>% slice(c(1,4,5)) %>% mutate(??)

Upvotes: 0

Views: 378

Answers (2)

r2evans
r2evans

Reputation: 160447

If you just want to subset those rows, then @Shree's answer is likely right. If you want to invert just those rows but otherwise keep all, then something like:

In 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
# ...

You said datatable, I think you meant data.table, in which case

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

Or pipe-wise as

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

In Base R, you can do it this way:

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

Upvotes: 1

Shree
Shree

Reputation: 11140

Here's one way -

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

Upvotes: 1

Related Questions