1

I have a Data Frame called "Data" which looks like this:

              Jobs       Agency      Location       Date RXH  HS TMM Payed
6     RWC Heineken    Lightblue           EGC 2015-10-10  90 8.5 765 FALSE
31     Playstation    Lightblue    Mirdiff CC 2015-11-13  90 7.0 630 FALSE
26    I.D Heineken    Lightblue Irish Village 2015-11-06  90 8.0 720 FALSE
35 Bank of America       Allure       Raffles 2015-11-17 100 3.0 300 FALSE
15 Netapp Gitex F1 Events House          DWTC 2015-10-20 100 8.0 800  TRUE

I want to change all items in Data$Payed where Data$Jobs == "RWC Heineken" to TRUE.

I could change it manually using fix(Data) but it would take me a few minutes as I need to change several of them.

I'm looking for a code that would do this automatically.

David Arenburg
  • 91,361
  • 17
  • 137
  • 196
  • 1
    Not sure about the expected output, `trimws(Data$Jobs)== "RWC Heineken"` – akrun Dec 14 '15 at 11:32
  • 2
    `Data$Payed[Data$Jobs == "RWC Heineken"] <- TRUE` ? – jogo Dec 14 '15 at 11:35
  • @jogo gave you the full answer. I found this post which is quite simillar http://stackoverflow.com/questions/11817371/replace-numbers-in-data-frame-column-in-r – leosz Dec 14 '15 at 11:46

1 Answers1

3

You need to assign a value based on the logic. Here's a work through:

Rows you want to change:

Data$Jobs == "RWC Heineken"
# Incidentally this tells you how many meet this criterion
sum(Data$Jobs == "RWC Heineken")

This returns the elements of the column you want to change:

Data$Payed[Data$Jobs == "RWC Heineken"]

Next step is changing the value where this logic applies:

Data$Payed[Data$Jobs == "RWC Heineken"] = "TRUE"

You could also:

Data[Data$Jobs == "RWC Heineken", "Payed"] = "TRUE"
MikeRSpencer
  • 1,276
  • 10
  • 24
  • Thanks Mike!!! Exactly what I needed, perfect explanation as well. Will this change my .csv file as well? – Tomas Alonso Rehor Dec 14 '15 at 11:46
  • Note that convention is to use `<-` for assignments. It can be (and has been) debated but for a beginner should know that `<-` is idiomatic R. – asachet Dec 14 '15 at 11:49
  • No, this only affects what you've loaded. Use 'write.csv' to output, although have a think about how you're managing data because this feels instinctively clumsy. – MikeRSpencer Dec 14 '15 at 11:50
  • The csv file won't be changed, if you want to rewrite the csv you can use `write.csv`. – asachet Dec 14 '15 at 11:50
  • I know antoine-sac, but I find `=` much easier to read and am yet to see a solid argument for always using <-. – MikeRSpencer Dec 14 '15 at 11:51