Convert a character to numeric

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Convert a character to numeric

bienvenidozoma@gmail.com
Hi, 

I am applyin function as.numeric to a vector having many values as NA 
and it is giving : 
Warning message: 
NAs introduced by coercion 

Can anyone help me to know how to remove this warning and sor it out? 

Thanks 
Bienvenue
        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: Convert a character to numeric

Jim Lemon-4
Hi Bienvenue,
Perhaps you should ask whether you really want to "sort it out". The
warning is telling you that you are converting the NA values to NA in
the returned numeric vector. I can't think of anything more sensible
to do with NA values. You may also have character strings that cannot
be converted into numbers, which will also generate NA values. Maybe a
little example will help us to understand:

charstr<-c("5","foot","2",NA,"eyes","of","blue")
as.numeric(charstr)
[1]  5 NA  2 NA NA NA NA

Jim

On Tue, Apr 9, 2019 at 9:16 PM [hidden email]
<[hidden email]> wrote:

>
> Hi,
>
> I am applyin function as.numeric to a vector having many values as NA
> and it is giving :
> Warning message:
> NAs introduced by coercion
>
> Can anyone help me to know how to remove this warning and sor it out?
>
> Thanks
> Bienvenue
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> 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.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: Convert a character to numeric

Duncan Murdoch-2
On 09/04/2019 7:30 a.m., Jim Lemon wrote:
> Hi Bienvenue,
> Perhaps you should ask whether you really want to "sort it out". The
> warning is telling you that you are converting the NA values to NA in
> the returned numeric vector.

I don't think that's what it is saying.  I think it is saying that a
non-NA value is being converted to NA, because R can't figure out what
number it is.  For example,

 > as.numeric(c("1", NA))
[1]  1 NA
 > as.numeric(c("1", NA, "one"))
[1]  1 NA NA
Warning message:
NAs introduced by coercion


  I can't think of anything more sensible
> to do with NA values. You may also have character strings that cannot
> be converted into numbers, which will also generate NA values.

I think that's the only way that message will appear.

Duncan Murdoch

  Maybe a

> little example will help us to understand:
>
> charstr<-c("5","foot","2",NA,"eyes","of","blue")
> as.numeric(charstr)
> [1]  5 NA  2 NA NA NA NA
>
> Jim
>
> On Tue, Apr 9, 2019 at 9:16 PM [hidden email]
> <[hidden email]> wrote:
>>
>> Hi,
>>
>> I am applyin function as.numeric to a vector having many values as NA
>> and it is giving :
>> Warning message:
>> NAs introduced by coercion
>>
>> Can anyone help me to know how to remove this warning and sor it out?
>>
>> Thanks
>> Bienvenue
>>          [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
>> 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.
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> 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.
>

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: Convert a character to numeric

Alfredo Cortell
Hi Bienvenue,

I believe that your problem is that R can't translate "one" to a number,
because it is not a number. R could translate to numeric for example this
vector, where numbers are expressed as strings,

c("1","4","7")

but "one" is just letters put together, therefore R can't understand their
meaning, and returns NA.

I believe that your best shot is to go with gsub, so that you translate
your "string letters" to "string numbers", and then you can do as.numeric
no problem

Try this

vec<-c("1",NA,"one")
num_vec<-gsub("one",1,vec)
num_vec<-as.numeric(num_vec)

Good luck,

Alfredo


El mar., 9 abr. 2019 a las 15:07, Duncan Murdoch (<[hidden email]>)
escribió:

> On 09/04/2019 7:30 a.m., Jim Lemon wrote:
> > Hi Bienvenue,
> > Perhaps you should ask whether you really want to "sort it out". The
> > warning is telling you that you are converting the NA values to NA in
> > the returned numeric vector.
>
> I don't think that's what it is saying.  I think it is saying that a
> non-NA value is being converted to NA, because R can't figure out what
> number it is.  For example,
>
>  > as.numeric(c("1", NA))
> [1]  1 NA
>  > as.numeric(c("1", NA, "one"))
> [1]  1 NA NA
> Warning message:
> NAs introduced by coercion
>
>
>   I can't think of anything more sensible
> > to do with NA values. You may also have character strings that cannot
> > be converted into numbers, which will also generate NA values.
>
> I think that's the only way that message will appear.
>
> Duncan Murdoch
>
>   Maybe a
> > little example will help us to understand:
> >
> > charstr<-c("5","foot","2",NA,"eyes","of","blue")
> > as.numeric(charstr)
> > [1]  5 NA  2 NA NA NA NA
> >
> > Jim
> >
> > On Tue, Apr 9, 2019 at 9:16 PM [hidden email]
> > <[hidden email]> wrote:
> >>
> >> Hi,
> >>
> >> I am applyin function as.numeric to a vector having many values as NA
> >> and it is giving :
> >> Warning message:
> >> NAs introduced by coercion
> >>
> >> Can anyone help me to know how to remove this warning and sor it out?
> >>
> >> Thanks
> >> Bienvenue
> >>          [[alternative HTML version deleted]]
> >>
> >> ______________________________________________
> >> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> >> 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.
> >
> > ______________________________________________
> > [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> > 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.
> >
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> 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.
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: Convert a character to numeric

Richard M. Heiberger
My guess is that numbers formatted with commas are causing an unwanted coercion.
Remove the commas with gsub before converting to numeric.

> as.numeric(NA)
[1] NA
> as.numeric("1,234,567")
[1] NA
Warning message:
NAs introduced by coercion
> as.numeric(gsub(",", "", "1,234,567"))
[1] 1234567
>

On Tue, Apr 9, 2019 at 10:36 AM Alfredo Cortell
<[hidden email]> wrote:

>
> Hi Bienvenue,
>
> I believe that your problem is that R can't translate "one" to a number,
> because it is not a number. R could translate to numeric for example this
> vector, where numbers are expressed as strings,
>
> c("1","4","7")
>
> but "one" is just letters put together, therefore R can't understand their
> meaning, and returns NA.
>
> I believe that your best shot is to go with gsub, so that you translate
> your "string letters" to "string numbers", and then you can do as.numeric
> no problem
>
> Try this
>
> vec<-c("1",NA,"one")
> num_vec<-gsub("one",1,vec)
> num_vec<-as.numeric(num_vec)
>
> Good luck,
>
> Alfredo
>
>
> El mar., 9 abr. 2019 a las 15:07, Duncan Murdoch (<[hidden email]>)
> escribió:
>
> > On 09/04/2019 7:30 a.m., Jim Lemon wrote:
> > > Hi Bienvenue,
> > > Perhaps you should ask whether you really want to "sort it out". The
> > > warning is telling you that you are converting the NA values to NA in
> > > the returned numeric vector.
> >
> > I don't think that's what it is saying.  I think it is saying that a
> > non-NA value is being converted to NA, because R can't figure out what
> > number it is.  For example,
> >
> >  > as.numeric(c("1", NA))
> > [1]  1 NA
> >  > as.numeric(c("1", NA, "one"))
> > [1]  1 NA NA
> > Warning message:
> > NAs introduced by coercion
> >
> >
> >   I can't think of anything more sensible
> > > to do with NA values. You may also have character strings that cannot
> > > be converted into numbers, which will also generate NA values.
> >
> > I think that's the only way that message will appear.
> >
> > Duncan Murdoch
> >
> >   Maybe a
> > > little example will help us to understand:
> > >
> > > charstr<-c("5","foot","2",NA,"eyes","of","blue")
> > > as.numeric(charstr)
> > > [1]  5 NA  2 NA NA NA NA
> > >
> > > Jim
> > >
> > > On Tue, Apr 9, 2019 at 9:16 PM [hidden email]
> > > <[hidden email]> wrote:
> > >>
> > >> Hi,
> > >>
> > >> I am applyin function as.numeric to a vector having many values as NA
> > >> and it is giving :
> > >> Warning message:
> > >> NAs introduced by coercion
> > >>
> > >> Can anyone help me to know how to remove this warning and sor it out?
> > >>
> > >> Thanks
> > >> Bienvenue
> > >>          [[alternative HTML version deleted]]
> > >>
> > >> ______________________________________________
> > >> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> > >> 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.
> > >
> > > ______________________________________________
> > > [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> > > 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.
> > >
> >
> > ______________________________________________
> > [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> > 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.
> >
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> 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.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: Convert a character to numeric

Boris Steipe
As there are many possible sources of the warning, to "sort it out" try something like

  which( is.na(<converted-vector>) & (! is.na(<original-vector>)))


B.



> On 2019-04-09, at 11:02, Richard M. Heiberger <[hidden email]> wrote:
>
> My guess is that numbers formatted with commas are causing an unwanted coercion.
> Remove the commas with gsub before converting to numeric.
>
>> as.numeric(NA)
> [1] NA
>> as.numeric("1,234,567")
> [1] NA
> Warning message:
> NAs introduced by coercion
>> as.numeric(gsub(",", "", "1,234,567"))
> [1] 1234567
>>
>
> On Tue, Apr 9, 2019 at 10:36 AM Alfredo Cortell
> <[hidden email]> wrote:
>>
>> Hi Bienvenue,
>>
>> I believe that your problem is that R can't translate "one" to a number,
>> because it is not a number. R could translate to numeric for example this
>> vector, where numbers are expressed as strings,
>>
>> c("1","4","7")
>>
>> but "one" is just letters put together, therefore R can't understand their
>> meaning, and returns NA.
>>
>> I believe that your best shot is to go with gsub, so that you translate
>> your "string letters" to "string numbers", and then you can do as.numeric
>> no problem
>>
>> Try this
>>
>> vec<-c("1",NA,"one")
>> num_vec<-gsub("one",1,vec)
>> num_vec<-as.numeric(num_vec)
>>
>> Good luck,
>>
>> Alfredo
>>
>>
>> El mar., 9 abr. 2019 a las 15:07, Duncan Murdoch (<[hidden email]>)
>> escribió:
>>
>>> On 09/04/2019 7:30 a.m., Jim Lemon wrote:
>>>> Hi Bienvenue,
>>>> Perhaps you should ask whether you really want to "sort it out". The
>>>> warning is telling you that you are converting the NA values to NA in
>>>> the returned numeric vector.
>>>
>>> I don't think that's what it is saying.  I think it is saying that a
>>> non-NA value is being converted to NA, because R can't figure out what
>>> number it is.  For example,
>>>
>>>> as.numeric(c("1", NA))
>>> [1]  1 NA
>>>> as.numeric(c("1", NA, "one"))
>>> [1]  1 NA NA
>>> Warning message:
>>> NAs introduced by coercion
>>>
>>>
>>>  I can't think of anything more sensible
>>>> to do with NA values. You may also have character strings that cannot
>>>> be converted into numbers, which will also generate NA values.
>>>
>>> I think that's the only way that message will appear.
>>>
>>> Duncan Murdoch
>>>
>>>  Maybe a
>>>> little example will help us to understand:
>>>>
>>>> charstr<-c("5","foot","2",NA,"eyes","of","blue")
>>>> as.numeric(charstr)
>>>> [1]  5 NA  2 NA NA NA NA
>>>>
>>>> Jim
>>>>
>>>> On Tue, Apr 9, 2019 at 9:16 PM [hidden email]
>>>> <[hidden email]> wrote:
>>>>>
>>>>> Hi,
>>>>>
>>>>> I am applyin function as.numeric to a vector having many values as NA
>>>>> and it is giving :
>>>>> Warning message:
>>>>> NAs introduced by coercion
>>>>>
>>>>> Can anyone help me to know how to remove this warning and sor it out?
>>>>>
>>>>> Thanks
>>>>> Bienvenue
>>>>>         [[alternative HTML version deleted]]
>>>>>
>>>>> ______________________________________________
>>>>> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
>>>>> 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.
>>>>
>>>> ______________________________________________
>>>> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
>>>> 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.
>>>>
>>>
>>> ______________________________________________
>>> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
>>> 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.
>>>
>>
>>        [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
>> 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.
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> 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.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.