ah bon
ah bon

Reputation: 10011

Filter rows based on multiple columns then manipulate another column in R

Given a dataframe df as follows:

df <- data.frame(city = c("bj", "bj", "bj", "bj", "sh", "sh", "sh", "sh", "sh" ),
                 type = c("a", "a", "b", "c", "a", "b", "c", "c", "a"),
                 count = c(100, 230, 12, 340, 17, 180, 25, 13, 12), stringsAsFactors = FALSE)

I would like to manipulate filter rows based on city and type: bj-a, bj-c, sh-b, then divide count's values by 10:

The expected result will like this:

city type count
bj  a   10      
bj  a   23      
bj  b   12      
bj  c   34      
sh  a   17      
sh  b   18      
sh  c   25      
sh  c   13      
sh  a   12

How could we do that in R? Thanks.

To filter rows, we may use:

df %>%
  filter(city == 'bj' & type %in% c('a', 'c') | 
         city == 'sh' & type == 'b')

Upvotes: 0

Views: 53

Answers (2)

akrun
akrun

Reputation: 887048

We can use case_when in dplyr

library(dplyr)
df %>%
     mutate(count = case_when(city == 'bj' & type %in% c('a', 'c') |
          city == 'sh' & type == 'b' ~ count/10, TRUE ~ as.numeric(count)))

Upvotes: 0

Ronak Shah
Ronak Shah

Reputation: 388907

You can use ifelse :

library(dplyr)
df %>%
  mutate(count = ifelse(city == 'bj' & type %in% c('a', 'c') | 
                        city == 'sh' & type == 'b', count/10, count))

This can also be done without ifelse :

df %>% mutate(count = count/c(1, 10)[(city == 'bj' & type %in% c('a', 'c') | 
                                      city == 'sh' & type == 'b') + 1])

Upvotes: 1

Related Questions