Changing Certain Values in a Dataset

Hello everyone!

Suppose that I have a dataset that looks like this:

ID Fuel Fuel_Fraction Rebate_USD
Unit 1 Gas 1 100
Unit 2 Electric 0.9 10
Unit 2 Gas 0.1 10
Unit 3 Oil 1 80
Unit 5 Gas 1 100
Unit 6 Electric 1 0

which suggests that units using gas or oil receive $100 and $80, respectively, and electric units do not get any rebates. Unit 2 is only getting $10 rebate because it only uses 10% gas (see "Fuel_Fraction" column) and 10% of $100 is $10.

I don't like the way that the data is formatted, so I wanna write a chunk of code that will make it look like this:

ID Fuel Fuel_Fraction Rebate_USD
Unit 1 Gas 1 100
Unit 2 Electric 0.9 0
Unit 2 Gas 0.1 10
Unit 3 Oil 1 80
Unit 5 Gas 1 100
Unit 6 Electric 1 0

That is, the "Rebate_USD" column for the second row (first Unit 2) is changed from $10 to $0.

Please not that the example above is a very simplified version of the actual dataset I'm using. The actual dataset is more complex and has more columns and rows. Therefore, it would be great if the solution could be as "flexible" or "general" as possible, so I can modify them relatively freely to suit my needs. In other words, the codes should not be too "rigid" to the point that it can ONLY be used to solve the example above and nothing else.

Of course, detailed explanation is always welcomed since I am quite new to R and still learning, haha.

Thank you in advance!

I would do this with a group_by and a case_when

df %>% 
  group_by(unit) %>% 
  mutate(
    rebate = case_when(
      any(fuel == "gas") ~ 100,
      any(fuel == "electric") ~ 80,
      TRUE  ~ 0
   )

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.