[R] converting character to numeric
Bill.Venables at csiro.au
Bill.Venables at csiro.au
Wed Jun 22 01:36:34 CEST 2011
The point I would make is that for safety it's much better to use FALSE rather than F. FALSE is a reserved word in R, F is a pre-set variable, but can easily be changed at any time by the user.
Secondly, doesn't this do the same as yours:
readFF.csv <- function(..., stringsAsFactors = FALSE)
read.csv(..., stringsAsFactors = stringsAsFactors)
? (Warning: untested code...)
Personally, I much prefer stringsAsFactors = TRUE, but then that's the way I was brought up...
Bill Venables.
-----Original Message-----
From: Don McKenzie [mailto:dmck at u.washington.edu]
Sent: Wednesday, 22 June 2011 8:40 AM
To: Venables, Bill (CMIS, Dutton Park)
Cc: stevenkennedy2263 at gmail.com; alinashe at gmail.com; r-help at r-project.org
Subject: Re: [R] converting character to numeric
I have to chime in here with a slight revision to read.csv(), for
those of us like me who have whined over time about "stringsAsFactors".
(tested on my machine macOSX 10.6)
readFF.csv <-
function (file, header = TRUE, sep = ",", quote = "\"", dec = ".",
fill = TRUE, comment.char = "", ...)
read.table(file = file, header = header, sep = sep, quote = quote,
dec = dec, fill = fill, stringsAsFactors=F,comment.char =
comment.char, ...)
On 21-Jun-11, at 3:16 PM, <Bill.Venables at csiro.au> wrote:
> ..or something like that. Without more details it is hard to know
> just what is going on.
>
> Firstly in R the object is a 'data frame' (or object of class
> "data.frame" to be formal). There is no standard object in R
> called a 'database'.
>
> If you read in your data using read.csv, then mydata is going to be
> a data frame. Character columns in the original .csv file will be
> (most likely) factors in the R object. (This varies with how you
> import it, though.) This means you will need to convert them to
> character before you convert them to numeric. If they really are
> character, this initial conversion will not do anything (good or bad).
>
> If you want to operate on the individual columns of the data frame,
> then I would recommend you do it using something like:
>
> mydata <- within(mydata, {
> apples <- as.numeric(as.character(apples))
> oranges <- as.numeric(as.character(oranges))
> .......
> })
>
> Bill Venables.
>
> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of Steven Kennedy
> Sent: Wednesday, 22 June 2011 6:52 AM
> To: Alina Sheyman
> Cc: r-help at r-project.org
> Subject: Re: [R] converting character to numeric
>
> You need:
> mydata$apples<-as.numeric(mydata$apples)
>
>
> On Wed, Jun 22, 2011 at 6:38 AM, Alina Sheyman <alinashe at gmail.com>
> wrote:
>> I'm trying to convert data from character to numeric.
>>
>> I've imported data as a csv file, I'm assuming that the import is a
>> database - are all the columns in a database considered
>> "vectors" and that
>> they can be operated on individually
>> Therefore I've tried the following
>> mydata <- as.numeric(mydata$apples)
>>
>> when i then look at mydata again the named column is still in
>> "character"
>> format
>> if i do mydata2 <- as.numeric(mydata$apples)
>> the new object mydata2 is empty.
>>
>> Am i missing something about the structure of R?
>>
>> alina
>>
>> [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posting-
>> guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-
> guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-
> guide.html
> and provide commented, minimal, self-contained, reproducible code.
Science is organized skepticism in the reliability of expert opinion
-- Richard Feynman
Don McKenzie, Research Ecologist
Pacific WIldland Fire Sciences Lab
US Forest Service
Affiliate Professor
School of Forest Resources, College of the Environment
CSES Climate Impacts Group
University of Washington
phone: 206-732-7824
dmck at uw.edu
More information about the R-help
mailing list