error in tapply: arguments must have same length

Daniel Svozil picture Daniel Svozil · Jun 4, 2015 · Viewed 27.8k times · Source

I have imported a set of data into R as a data frame from a .csv file. Originally, I had an error message as follows:

> data<-read.csv("D:/research/PhD 2014/Data/anaesthesia trials/anaesthesia    times.csv", header=TRUE)
> str(data)
'data.frame':   80 obs. of  10 variables:
$ chemical: Factor w/ 2 levels "aquis","benzocaine": 1 1 1 1 1 1 1 1 1 1 ...
$ Fish    : int  1 2 3 4 5 6 7 8 9 10 ...
$ Dose    : int  10 10 10 10 10 10 10 10 10 10 ...
$ X1      : int  442 0 0 0 0 0 0 0 0 0 ...
$ X2      : int  600 0 0 0 0 0 0 0 0 0 ...
$ X3a     : int  885 0 0 0 0 0 0 0 0 0 ...
$ X3b     : int  0 0 0 0 0 0 0 0 0 0 ...
$ Recovery: int  650 0 0 0 0 0 0 0 0 0 ...
$ wt..g.  : num  0.19 0 0 0 0 0 0 0 0 0 ...
$ tl..mm. : int  35 0 0 0 0 0 0 0 0 0 ...
> Mean<-tapply(data$x1,data$Dose, mean)
 Error in tapply(data$x1, data$Dose, mean) : 
    arguments must have same length
> length(data$Dose)
 [1] 80
> length(data$x1)
 [1] 0
> dim(data$x1)
 NULL

I suspect that this is because there are missing values. However, the missing values are not missing values as such, they are real 0's. I have trawled for the error and the function 'tapply' here, on other forums, and actually copied the code from "A beginners guide to R" and replaced it with my own data.

My other concern, which is more problematic than missing zeroes is that 'x1' has a length of '0' according the the length() function...

I am quite puzzled because I have gone back and checked the .csv file, tried saving it as another .csv file and nothing appears to change. I have made sure to change the cell formatting to "number" in the .csv itself and this doesn't appear to have helped. Is R not recognizing data for some reason?

I can provide the problem .csv file, just not sure what would be the best way to do it.

Answer

cyberj0g picture cyberj0g · Jun 4, 2015

You should use data$X1 (uppercase X) in tapply call. Names in R are case-sensitive. Let us know if it worked.