-1

I have a tibble:

library('tibble')

df <- tibble(
  ticker = c("first", "second", "third"),
  status = c(T,T,T)
)

> df
# A tibble: 3 x 2
  ticker status
 1 first  TRUE  
 2 second TRUE  
 3 third  TRUE  

I want to change the status of 'first' to FALSE in the original df.

But when I run this code:

library('dplyr')

df %<>%
  filter(ticker=='first') %>%
  mutate(
   status = F
  )

I get my original df only with the first row

> df
# A tibble: 1 x 2
  ticker status
1 first  FALSE 

Instead of:

> df
# A tibble: 3 x 2
  ticker status
1 first  FALSE 
2 second TRUE  
3 third  TRUE
Oktu
  • 167
  • 1
  • 4
  • 10

2 Answers2

2

Using base R

 within(df, status[ticker == "first"] <- FALSE )

Humpelstielzchen
  • 5,403
  • 3
  • 9
  • 32
0

Using the function case_when:

library('tibble')

df <- tibble(
    ticker = c("first", "second", "third"),
    status = c(T,T,T)
)

library(tidyverse)

df %>% 
    mutate(status = case_when(
        ticker == "first" ~ F,
        TRUE ~ T
    ))

This is the output:

# A tibble: 3 x 3
  ticker status `case_when(ticker == "first" ~ F, TRUE ~ T)`
  <chr>  <lgl>  <lgl>                                       
1 first  TRUE   FALSE                                       
2 second TRUE   TRUE                                        
3 third  TRUE   TRUE 
Scipione Sarlo
  • 1,328
  • 1
  • 15
  • 23