[R] Numbers not numeric?

Peter Langfelder peter.langfelder at gmail.com
Sat Apr 21 00:07:44 CEST 2012


On Fri, Apr 20, 2012 at 12:44 PM, Charles Determan Jr <deter088 at umn.edu> wrote:
> Greetings R users,
>
> I have a curious problem.  I read in a csv file (subset shown below) as
> normal
> data=read.table("C:/Users/Chaz/Desktop/test.csv",sep=",",header=TRUE,
> na.strings=".")
>
> However, the numbers from the dataset are not registered as numeric:
>
> is.numeric(data$Mesh)
> [1] FALSE
>
> When I try as.numeric, it converts all the values to different integers.
> This changes the analysis as I would like calculate simple things such as
> the means and standard deviation.  Any thoughts would be appreciated.
>
>
>  Mesh SubQ Edge  90 10 55  60 25 45  60 12 85  50 50 90  45 80 70  40 45 45
> 80 40 65  100 65 58

Two issues:

1. Some of your data in the Mesh column are non-numeric (entries
"SubQ" and "Edge"). This causes the entire column to be read in as
character strings.

2. The columns gets read in as character strings and is by default
converted to a factor (see ?factor for information about factors). To
convert the actual character strings to numbers, you need to use
as.numeric(as.character(data$Mesh)). as.numeric returns the underlying
codes that factor uses for each level. You can disable converting
character strings into factors by using

options(stringsAsFactors = FALSE)

before the call to read.csv().

HTH,

Peter



More information about the R-help mailing list