2

I have a dataset with two (well, actually a lot more, but they're irrelevant) numeric variables. I want to compare the value in the second variable to the (corresponding) value in the firsts value. The values in the second variable should be lower than those in the first variable. If this is not the case I want this to result in a value '1' (error) in the third variable, that indicates if the comparison is succesful or not (error: yes/no)

However, the values in both columns might differ in the number of decimals. So these should first be adjusted (each value in variable 2 should have the same number of decimals as the corresponding value in variable 1). For this I have used the function below.

Is there a convenient method for making this comparison in R?

Example of data:

var1  var2
0.06  0.060008314
0.01  0.007975829
0.03  0.034835578
0.03  0.032115433
Community
  • 1
  • 1
mats
  • 133
  • 1
  • 3
  • 10

2 Answers2

3

Like this?

dat = data.frame(x=runif(10), y=runif(10))
> dat
            x          y
1  0.54924947 0.26023483
2  0.89064477 0.28528469
3  0.87488691 0.18475596
4  0.27606585 0.49777871
5  0.19463634 0.59677062
6  0.52419706 0.62171800
7  0.44588382 0.55170973
8  0.07009947 0.71273801
9  0.25127679 0.24720947
10 0.04094697 0.08151144
> dat$error = ifelse(dat$y<dat$x,1,0)
> dat
            x          y error
1  0.54924947 0.26023483     1
2  0.89064477 0.28528469     1
3  0.87488691 0.18475596     1
4  0.27606585 0.49777871     0
5  0.19463634 0.59677062     0
6  0.52419706 0.62171800     0
7  0.44588382 0.55170973     0
8  0.07009947 0.71273801     0
9  0.25127679 0.24720947     1
10 0.04094697 0.08151144     0
Davy Kavanagh
  • 4,809
  • 9
  • 35
  • 50
1

Edit: Updated method for counting digits after decimal that also works for numbers represented in scientific notation (borrowed from how to return number of decimal places in R)

# sample data
x <- data.frame(var1 = c(0.06, 0.01, 0.03, 0.03), 
                var2 = c(0.060008314, 0.007975829, 0.034835578, 0.032115433))

x$var3 <- 0 # first set all var3 to zero
# figure out how many digits after decimal in var1
x$dec <- nchar(sub("^.+[.]", "", sub("0+$", "", as.character(x$var1))))    
# if var1 is <= rounded var2, set equal to 1
x[x$var1 <= round(x$var2, x$dec), 'var3'] <- 1

After rounding, all var1 == var2, so no var2 is less than var 1.

>     x[, 1:3]
  var1        var2 var3
1 0.06 0.060008314    1
2 0.01 0.007975829    1
3 0.03 0.034835578    1
4 0.03 0.032115433    1
Community
  • 1
  • 1
GSee
  • 48,880
  • 13
  • 125
  • 145