vendredi 3 janvier 2020

Changing the value of one column based on another in r

I have the following data, and I want to change the values in the count column to negative if the text in 'Color' is Yellow. I tried mutate_if with no success. How can I go about doing so if I want to:

  1. Replace the values in count with the new negative values, or
  2. Create a new column with the name Count_final with new negative values, as well as the same old values for the entries that are not yellow?

Thank you.

data <- tibble::tribble(
    ~Color,    ~Item, ~Count,       ~Year,
    "Blue",    "Bag",     50, "2009-2011",
    "Blue", "Wallet",     60, "2009-2011",
   "Green",  "Shoes",     80, "2009-2011",
   "Green",  "Shirt",     90, "2009-2011",
  "Yellow", "Flower",     20, "2009-2011",
  "Yellow",   "Bees",     30, "2009-2011",
    "Blue",    "Bag",     50, "2009-2011",
    "Blue", "Wallet",     60, "2009-2011",
   "Green",  "Shoes",     90, "2009-2011",
   "Green",  "Shirt",     20, "2009-2011",
  "Yellow", "Flower",     10, "2009-2011",
  "Yellow",   "Bees",      5, "2009-2011"
  )

Aucun commentaire:

Enregistrer un commentaire