Gustavo
Gustavo

Reputation: 49

R output giving wrong values for difference between columns

I have this tibble called data1:

structure(list(subject = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 
12), treatment = c("099526-01", "099526-01", "099526-01", "099526-01", 
"099526-01", "099526-01", "099526-01", "099526-01", "099526-01", 
"099526-01", "099526-01", "099526-01"), T0 = c(34.35, 26.5, 29.65, 
11.575, 34.4, 25.775, 33, 31.6, 18.35, 36.275, 36.075, 34.225
), T15min = c(34.85, 28.95, 30.2, 11.05, 34.1, 22.025, 25.325, 
31.775, 17.8, 31.7, 35.35, 34.25), T2h = c(33.425, 26.125, 27.65, 
11.475, 36.95, 22.975, 30.025, 31.775, 18.025, 33.025, 34.125, 
34.55), T4h = c(35.7, 26.075, 29.3, 13.275, 36.45, 28.475, 30.925, 
32.15, 17.425, 34.95, 34.55, 34.775), T6h = c(36.225, 28.15, 
29.1, 12.25, 34.275, 26.05, 28.1, 34.025, 17.775, 35.3, 35.125, 
36.725), T8h = c(34.9, 25.75, 30.425, 10.75, 34.425, 28.725, 
28.475, 34.35, 19.325, 33.925, 36.95, 38.225)), row.names = c(NA, 
-12L), class = c("tbl_df", "tbl", "data.frame"))

 subject treatment    T0 T15min   T2h   T4h   T6h   T8h
     <dbl> <chr>     <dbl>  <dbl> <dbl> <dbl> <dbl> <dbl>
 1       1 099526-01  34.4   34.8  33.4  35.7  36.2  34.9
 2       2 099526-01  26.5   29.0  26.1  26.1  28.2  25.8
 3       3 099526-01  29.6   30.2  27.6  29.3  29.1  30.4
 4       4 099526-01  11.6   11.0  11.5  13.3  12.2  10.8
 5       5 099526-01  34.4   34.1  37.0  36.4  34.3  34.4
 6       6 099526-01  25.8   22.0  23.0  28.5  26.0  28.7
 7       7 099526-01  33     25.3  30.0  30.9  28.1  28.5
 8       8 099526-01  31.6   31.8  31.8  32.2  34.0  34.4
 9       9 099526-01  18.4   17.8  18.0  17.4  17.8  19.3
10      10 099526-01  36.3   31.7  33.0  35.0  35.3  33.9
11      11 099526-01  36.1   35.4  34.1  34.6  35.1  37.0
12      12 099526-01  34.2   34.2  34.6  34.8  36.7  38.2

I'm creating a new tibble with new columns as the difference of times to T0 (e.g., T15min-T0, T2h-T0), as follows:

data2 <- data1 %>%
    mutate(delta_1 = .[[4]] - .[[3]], 
           delta_2 = .[[5]] - .[[3]],
           delta_3 = .[[6]] - .[[3]],
           delta_4 = .[[7]] - .[[3]],
           delta_5 = .[[8]] - .[[3]])

 subject treatment    T0 T15min   T2h   T4h   T6h   T8h delta_1 delta_2 delta_3 delta_4 delta_5
     <dbl> <chr>     <dbl>  <dbl> <dbl> <dbl> <dbl> <dbl>   <dbl>   <dbl>   <dbl>   <dbl>   <dbl>
 1       1 099526-01  34.4   34.8  33.4  35.7  36.2  34.9  0.5     -0.925   1.35    1.88   0.550 
 2       2 099526-01  26.5   29.0  26.1  26.1  28.2  25.8  2.45    -0.375  -0.425   1.65  -0.75  
 3       3 099526-01  29.6   30.2  27.6  29.3  29.1  30.4  0.550   -2      -0.350  -0.550  0.775 
 4       4 099526-01  11.6   11.0  11.5  13.3  12.2  10.8 -0.525   -0.100   1.70    0.675 -0.825 
 5       5 099526-01  34.4   34.1  37.0  36.4  34.3  34.4 -0.300    2.55    2.05   -0.125  0.0250
 6       6 099526-01  25.8   22.0  23.0  28.5  26.0  28.7 -3.75    -2.80    2.70    0.275  2.95  
 7       7 099526-01  33     25.3  30.0  30.9  28.1  28.5 -7.68    -2.98   -2.07   -4.9   -4.52  
 8       8 099526-01  31.6   31.8  31.8  32.2  34.0  34.4  0.175    0.175   0.550   2.43   2.75  
 9       9 099526-01  18.4   17.8  18.0  17.4  17.8  19.3 -0.550   -0.325  -0.925  -0.575  0.975 
10      10 099526-01  36.3   31.7  33.0  35.0  35.3  33.9 -4.57    -3.25   -1.32   -0.975 -2.35  
11      11 099526-01  36.1   35.4  34.1  34.6  35.1  37.0 -0.725   -1.95   -1.53   -0.950  0.875 
12      12 099526-01  34.2   34.2  34.6  34.8  36.7  38.2  0.0250   0.325   0.550   2.50   4 

However, the differences are not correct. For example, for the first subject, T2h - T0 (33.4 - 34.4) should result -1, and not -0.925

What could be wrong with the code?

Upvotes: 0

Views: 32

Answers (1)

uow
uow

Reputation: 130

The code is correct. However, it appears that the df output (View) is limited to 1 decimal, while your values have 3 decimals.

Try running

options(digits = 5)

at the top of your script.

Upvotes: 1

Related Questions