-2

this is how my data frame looks

and this is what I am doing:

enter code here
nw_dat$cost_of_2_AUD<-if ( nw_dat$Country=="India") {
nw_dat$cost_of_2_AUD <- nw_dat$Average.Cost.for.two/56.7
} else if (nw_dat$Country=="Phillipines") {
 nw_dat$cost_of_2_AUD <- nw_dat$Average.Cost.for.two/36.97
} else if ( nw_dat$Country=="Brazil") {
  nw_dat$cost_of_2_AUD <- nw_dat$Average.Cost.for.two/4.08
}else if ( nw_dat$Country=="Canada") {
nw_dat$cost_of_2_AUD <- nw_dat$Average.Cost.for.two/0.94
}else if ( nw_dat$Country=="Indonesia") {
nw_dat$cost_of_2_AUD <- nw_dat$Average.Cost.for.two/11066.12
}else if ( nw_dat$Country=="New Zealand") {
nw_dat$cost_of_2_AUD <- nw_dat$Average.Cost.for.two/1.08
}else if ( nw_dat$Country=="Singapore") {
nw_dat$cost_of_2_AUD <- nw_dat$Average.Cost.for.two/1.03
}else if ( nw_dat$Country=="South Africa") {
nw_dat$cost_of_2_AUD <- nw_dat$Average.Cost.for.two/10.92
}else if ( nw_dat$Country=="Sri Lanka") {
nw_dat$cost_of_2_AUD <- nw_dat$Average.Cost.for.two/152.35
}else if ( nw_dat$Country=="Turkey") {
nw_dat$cost_of_2_AUD <- nw_dat$Average.Cost.for.two/6.57
}else if ( nw_dat$Country=="UAE") {
 nw_dat$cost_of_2_AUD <- nw_dat$Average.Cost.for.two/2.84
}else if ( nw_dat$Country=="United Kingdom") {
 nw_dat$cost_of_2_AUD <- nw_dat$Average.Cost.for.two/0.55
}else if ( nw_dat$Country=="United States") {
  nw_dat$cost_of_2_AUD <- nw_dat$Average.Cost.for.two/0.77
}else {
  nw_dat$cost_of_2_AUD <- nw_dat$Average.Cost.for.two

}

but the problem is it's not converting it to the correct value, only the Philippines values that I converted are correct but the rest of the country's values are wrong.

Thank you in advance for the help.

  • 4
    It would be easier to help if you create a small reproducible example along with expected output. Read about [how to give a reproducible example](http://stackoverflow.com/questions/5963269). Images are not the right way to share data/code. – Ronak Shah May 14 '21 at 04:44

1 Answers1

2

Create a lookup table with country name and corresponding value to divide for that country.

#Include all the countries, I am showing only 3 as example. 
lookup <- data.frame(Country = c('India', 'Phillipines', 'Brazil'), 
                     value = c(56.7, 36.97, 4.08))

merge the dataframe with nw_dat and directly divide the value.

nw_dat <- transform(merge(nw_dat, lookup, by = 'Country'), 
                    cost_of_2_AUD = Average.Cost.for.two/value)
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213