Skip to content

Error occurred during mean calculation of a column of a data frame, which is apparently contents numeric data

7 messages · Aniruddha Mukherjee, Berend Hasselman, R. Michael Weylandt +2 more

#
On 29-02-2012, at 09:45, Aniruddha Mukherjee wrote:

            
nrows? 
I assume this was set somewhere in your script and not shown.
Is it length(str_got)?
Use matr1 <- as.data.frame(matr, stringsAsFactors=FALSE)

If you don't dos tringsAsFactors=FALSE the column will be a factor and that is not equivalent with numeric.

What's wrong with

matr1$Pulse_rate <- as.numeric(matr1$Pulse_rate)

Then you can calculate the desired mean with 

mean(matr1$Pulse_rate)

or

mean(matr1[,"Pulse_rate"])

Berend
#
On 29-02-2012, at 11:49, Aniruddha Mukherjee wrote:

            
?factor

and play a little bit

as.factor(c("A","8.9"))
as.numeric(as.factor(c("A","8.9")))
str(as.factor(c("A","8.9")))

and read the R Intro manual (chapter 4).

Berend
#
Factors are internally stored as integers (enums if you have used
other programming languages) with a special label set -- it's more
memory efficient than storing the whole string over and over.

Michael

On Wed, Feb 29, 2012 at 5:49 AM, Aniruddha Mukherjee
<aniruddha.mukherjee at tcs.com> wrote:
#
On 12-02-29 8:16 AM, R. Michael Weylandt wrote:
That was one of the original justifications, but character vectors are 
just as memory efficient these days.

The other justifications are still valid:  sometimes you have a vector 
which only takes on a subset of the possible values it could take, and 
when you tabulate it, you'd like to see those zero counts.  You may also 
want to control the display order, and a factor allows that.

For example:

x <- c("a", "a", "b")
table(x)
x <- factor(x, levels=c("c", "b", "a"))
table(x)

Duncan Murdoch
#
On 29/02/2012 13:41, Duncan Murdoch wrote:
No, not really.  Character vectors (STRSXPs) store a pointer for each 
string entry, and factors store an integer.  On most current systems 
pointers are twice the size of integers, so on a 64-bit system

 > a <- rep(letters[1:10], each = 1000)
 > object.size(a)
80520 bytes
 > object.size(as.factor(a))
41008 bytes