[R] converting character to numeric

Denis Kazakiewicz d.kazakiewicz at gmail.com
Wed Jun 22 00:32:39 CEST 2011


IMHO: 'as.numeric(as.character'   is very reasonable suggestion
On 22.06.2011 01:16, 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.



More information about the R-help mailing list