0

I have a dataset in R that is structured like so:

> head(top10_master, n = 5)
   sensitivity      PRODUCT NewRevenue NewQuantity NewProfit Change_Price Rev_Change Prof_Change Quant_Change Revenue Quantity  Profit
1:           1 PRODUCT33411    7067750    11809634   5790148      0.80000  1.2282115    1.173265    1.5591315 5754505  7574495 4935074
2:           1 PRODUCT35481    4484178     5721789   3278738      1.04978  1.0000000    1.000000    1.0000000 4484178  5721789 3278738
3:           1 PRODUCT26835    5239519     2239783   3950871      0.90000  1.1801825    1.125136    1.3884444 4439584  1613160 3511460
4:           1 PRODUCT33203    5872765     1139270   3125781      0.90000  1.3477930    1.161484    1.6487265 4357320   691000 2691195
5:           1 PRODUCT22426    4125707     1121494   3249458      0.80000  0.9590297    1.378040    0.4507613 4301960  2488000 2358029

I am trying to edit the Change_Price column to 1 (inside of a function) in the instances where the Rev_Change and Quant_Change columns == 1. I have tried every method I could find including using dplyr but for some reason none of these methods are working and the output is always the same. Please help.

I have tried too many methods to list them all but among them are all of the methods listed in this post. For some reason, none of these methods is working.

zsad512
  • 861
  • 3
  • 15
  • 41
  • Perhaps it is a case of floating point accuracy – akrun Dec 21 '17 at 17:49
  • @akrun I suspect you may be right because it seems like traditional subsetting is not recognizing `Change_Rev = 1` etc... how would I fix that? – zsad512 Dec 21 '17 at 19:16

2 Answers2

0

As akrun suggested, maybe there is a floating point accuracy issue. You could try using dplyr's near:

This is a safe way of comparing if two vectors of floating point numbers are (pairwise) equal. This is safer than using ==, because it has a built in tolerance.

This might work for you:

library(dplyr)
top10_master$Change_Price[which(near(top10_master$Rev_Change, 1) & near(top10_master$Quant_Change, 1))] <- 1
Hallie Swan
  • 2,714
  • 1
  • 15
  • 23
-1
top10_master$Change_Price[sapply(top10_master$Rev_Change, all.equal, 1) & sapply(top10_master$Quant_Change, all.equal, 1)] <- 1

Subset to the data you want to change and then assign.

troh
  • 1,354
  • 10
  • 19
  • as mentioned in my post @troh I have tried this and it didnt work – zsad512 Dec 21 '17 at 19:17
  • @zsad512 I've updated my answer from the comment above. – troh Dec 21 '17 at 22:32
  • `Error in sapply(inData$Change_Rev, all.equal, 1) & sapply(inData$Change_Quant, : operations are possible only for numeric, logical or complex types` – zsad512 Dec 21 '17 at 23:08
  • I checked the input data tables as well before posting ^ and they are all listed as `numeric` so not really sure whats going on here... – zsad512 Dec 21 '17 at 23:14
  • I'm assuming you used: `is.numeric(inData$Change_Rev); is.numeric(inData$Change_Quant)` – troh Dec 22 '17 at 14:32