This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Finding The Mean Of Values In A Single Column

I have a data frame (process.yield):

process    Yield
35        0.38
37        0.29
89        0.75
90        0.82

I want R to calculate the mean of values in column 2 ("Yield"). This seems trivial, but somehow after applying functions like apply, aggregate, mean, by, I have not been able to get the right results. I'm guessing there is a problem with my data frame.

Example: Aggregate function:

process.yield.mean <- aggregate(process.yield, by=list(process.yield$Yield), FUN=mean)

Error from aggregate function:

1: In mean.default(X[[1L]], ...) :
 argument is not numeric or logical: returning NA 
  etc etc

Can anyone help please?

r

The error message is telling you that the thing you are passing to mean() isn't a numeric or a logical vector. Use class() to work out what your column is (most likely a character vector?) and convert it. If you are reading this data in from .csv you may find some of the entries in that column are not, in fact, numbers?

2 answers

How about just mean(process.yield$Yield)? That would seem rather simpler.

mean(process.yield$Yield) gave this error: [1] NA Warning message: In mean.default(process.yield$Yield) : argument is not numeric or logical: returning NA

What about mean(as.numeric(process.yield$Yield))?

It's actually probably a factor, in which as.numeric(levels(x))[x] is the way to go. As per ?factor. In any case, the correct diagnosis for the problem is in the error message...

That was exceptional David. as.numeric(levels(x))[x] did it. Why do you think "factor" was introduced, since all I did was to to use cbind() to combine the 2 columns in the data frame.

It's a bit complex, but cbind() and rbind() return matrices, which can only contain one data-type and will convert numerics to characters if there are some in the things that are being bound. as.data.frame() converts character vectors to factors by default. If you have mixed types t's usually best to use data.frame(x=my_numeric, y = my char, z=my_factors)

Then as David W. suggested above, those are probably characters, not numbers. Try converting with as.numeric().

summary (process.yield) command will also give mean.

Thanks all for all your comments. mean() and summary() should have worked, but so far this has not happened, and I'm suspecting the way I put together the data frame in the first place. Process.Yield frame was obtained by combining Process and Yield columns using cbind(). mean() worked fine with Yield column before being combined with Process column.

Log in to answer this question.