0

I've used tapply() a lot in R, but I have no idea why the order of magnitudes are suddenly converted after tapply() function is applied.

When I load the original CSV data, the data shows as follows.

Barcode        Group     Price
1002-01-23       A       10.23568975
1002-01-24       A       2356.25
1002-01-25       A       123.54897
1002-01-26       A       200.1548794

However, after I use R codes, the digits of Price are converted as follows.

Barcode        Group     Price         mean
1002-01-23       A       10.23569      672.5474
1002-01-24       A       2356.25000    672.5474
1002-01-25       A       123.54897     672.5474
1002-01-26       A       200.15488     672.5474

I would like to have 672.5473847875(=(10.23568975+2356.25+123.54897+200.1548794)/4) as a result of mean. How could I solve the problem? Let me show you my R codes.

barcode <- read.csv("barcode.csv",header=T)
barcode$Group <- as.factor(barcode$Group)
barcode$Price <- as.numeric(barcode$Price)
test <- tapply(barcode$Price, barcode$Group, mean)
test1 <- data.frame(Group=names(test), mean=test)
barcode$mean <- test1$mean[match(barcode$Group, test1$Group)]

I really need your help. Thank you so much.

Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294
seulki choi
  • 45
  • 1
  • 8
  • 2
    This is almost certainly related to printing the values rather than the values that are stored internally. For example, `temp <- 1.00045346643` and then in console, type `temp`. Then try `print(temp, digits=10)`. – lmo Oct 12 '17 at 12:42
  • Please save the RStudio tag for problems specific to RStudio - for example if you had code that ran fine in RGui but didn't work in RStudio. – Gregor Thomas Oct 12 '17 at 13:42

1 Answers1

1

The means are calculated correctly. The simplest way to see this is to test for it:

barcode$mean == 672.5473847875
[1] TRUE TRUE TRUE TRUE

You can change the default number of digits being printed by e.g.

options(digits=15)
Otto Kässi
  • 2,943
  • 1
  • 10
  • 27