[R] Error occurred during mean calculation of a column of a data frame, which is apparently contents numeric data
Berend Hasselman
bhh at xs4all.nl
Wed Feb 29 11:26:04 CET 2012
On 29-02-2012, at 09:45, Aniruddha Mukherjee wrote:
> Hello R people,
>
> How can I compute the mean of the "Pulse_rate" column of the data frame or
> matrix from the following character object called "str_got". It has 14
> entries and each entry has 8 values, separated by commas. Please go thru
> the following R commands to know how I tried to unstring and unlist the
> values to form a data frame.
>> str_got
> [1] "bp,67,2011-12-09T19:59:44.044+05:30,9830576102,68.0,124.0,58.0,66.0"
> "bp,67,2011-12-09T20:19:31.031+05:30,9830576102,72.0,133.0,93.0,40.0"
> .....
>>
> matr<-matrix(unlist(strsplit(str_got, ",")), nrows, byrow=T)
nrows?
I assume this was set somewhere in your script and not shown.
Is it length(str_got)?
>> matr
> [,1] [,2] [,3]
> [,4] [,5] [,6] [,7] [,8]
> [1,] "bp" "67" "2011-12-09T19:59:44.044+05:30" "9830576102" "68.0"
> ......
> Note column names must be inserted before computing the desired mean
> value.
> matr1<-as.data.frame(matr)
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
More information about the R-help
mailing list