简体   繁体   中英

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')

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])

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)))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM