Home > front end >  Conditional Mutating using specific identifiers in a data frame
Conditional Mutating using specific identifiers in a data frame

Time:07-06

I have a dataset that employs a similar approach to this one.

ID <- c(4,5,6,7,3,8,9)
quantity <- c(20,300, 350, 21, 44, 20, 230)
measurementvalue <- c("tin", "kg","kg","tin","tin","tin","kg")
kgs <- c(21,12, 30,23,33,11,24)

DF <- data.frame(ID, quantity, measurementvalue)

My standard way of deriving a new column totalkgs using conditional mutating is the code below.

DF <- DF %>%
  mutate(totalkgs = 
           ifelse(quantity == "tin", quantity * 5, 
                  ifelse(quantity =="kg", quantity*1, quantity)))

However, the dataset had erroneous entries in the column quantity so I'd like to perform a division on those specific identifiers. All the final values of both the multiplication and division should be store in the column totalkgs. How do I go about this?

Let's assume the id's with the error data are 3,5,9,7. and I'd like to divide the values found in the column quantity with 10.

CodePudding user response:

You could use case_when:

ID <- c(4,5,6,7,3,8,9)
quantity <- c(20,300, 350, 21, 44, 20, 230)
measurementvalue <- c("tin", "kg","kg","tin","tin","tin","kg")
kgs <- c(21,12, 30,23,33,11,24)
DF <- data.frame(ID, quantity, measurementvalue)

library(dplyr)
DF %>%
  mutate(quantity2 = ifelse(ID %in% c(3,5,9,7), quantity/10, quantity)) %>%
  mutate(totalkgs = case_when(measurementvalue == "tin" ~ quantity2 * 5,
                              measurementvalue == "kg" ~ quantity2 * 1, 
                              TRUE ~ quantity2)) %>%
  select(-quantity2) #if you want
#>   ID quantity measurementvalue totalkgs
#> 1  4       20              tin    100.0
#> 2  5      300               kg     30.0
#> 3  6      350               kg    350.0
#> 4  7       21              tin     10.5
#> 5  3       44              tin     22.0
#> 6  8       20              tin    100.0
#> 7  9      230               kg     23.0

Created on 2022-07-05 by the reprex package (v2.0.1)

  • Related