converting factor to numeric gives "NAs introduced by coercion"

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

converting factor to numeric gives "NAs introduced by coercion"

anglor
Hi,

I have a dataframe that I imported from a .txt file by:

skogTemp <- read.delim2("Skogaryd_shoot_data.txt", header=TRUE, fill=TRUE)

and the data are factors, how can avoid factors from the beginning? Although the file contains both characters and numbers.

I tried to convert some of the columns from factor to numeric and as I understood it you can not use only as.numeric but as.character first. I got this warning message:

> skogTemp_1 <- as.numeric(as.character(skogTemp_1[,2:4]))
Warning message:
NAs introduced by coercion

I have lots of NAs in my data. Tries to check what class I had now but another warning is given me:
> class(skogTemp_1[,2])
Error in skogTemp_1[, 2] : incorrect number of dimensions
> class(skogTemp_1[1,2])
Error in skogTemp_1[1, 2] : incorrect number of dimensions

frustrating... I don't know what this mean.

Can anyone help?

Thank you,
Angelica

Reply | Threaded
Open this post in threaded view
|

Re: converting factor to numeric gives "NAs introduced by coercion"

djmuseR
Hi:

The argument stringsAsFactors = FALSE will suppress character data
from being converted to factors. For more control, see the arguments
colClasses and as.is in ?read.table - they should carry over to
read.delim[2] but don't quote me on that :)

HTH,
Dennis

On Fri, Jul 29, 2011 at 1:45 AM, anglor <[hidden email]> wrote:

> Hi,
>
> I have a dataframe that I imported from a .txt file by:
>
> skogTemp <- read.delim2("Skogaryd_shoot_data.txt", header=TRUE, fill=TRUE)
>
> and the data are factors, how can avoid factors from the beginning? Although
> the file contains both characters and numbers.
>
> I tried to convert some of the columns from factor to numeric and as I
> understood it you can not use only as.numeric but as.character first. I got
> this warning message:
>
>> skogTemp_1 <- as.numeric(as.character(skogTemp_1[,2:4]))
> Warning message:
> NAs introduced by coercion
>
> I have lots of NAs in my data. Tries to check what class I had now but
> another warning is given me:
>> class(skogTemp_1[,2])
> Error in skogTemp_1[, 2] : incorrect number of dimensions
>> class(skogTemp_1[1,2])
> Error in skogTemp_1[1, 2] : incorrect number of dimensions
>
> frustrating... I don't know what this mean.
>
> Can anyone help?
>
> Thank you,
> Angelica
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/converting-factor-to-numeric-gives-NAs-introduced-by-coercion-tp3703408p3703408.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] 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.
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Odp: converting factor to numeric gives "NAs introduced by coercion"

PIKAL Petr
In reply to this post by anglor
Hi

> Hi,
>
> I have a dataframe that I imported from a .txt file by:
>
> skogTemp <- read.delim2("Skogaryd_shoot_data.txt", header=TRUE,
fill=TRUE)
>
> and the data are factors, how can avoid factors from the beginning?
Although
> the file contains both characters and numbers.

You have got an answer but here are some comments. If you have characters
and numbers in one column the character values are converted to NA by
as.numeric

>
> I tried to convert some of the columns from factor to numeric and as I
> understood it you can not use only as.numeric but as.character first. I
got
> this warning message:
>
> > skogTemp_1 <- as.numeric(as.character(skogTemp_1[,2:4]))
> Warning message:
> NAs introduced by coercion

What is skogTemp_1? I presume skogTemp is data frame and in that case you
can not use such construction directly.

>
> I have lots of NAs in my data. Tries to check what class I had now but
> another warning is given me:
> > class(skogTemp_1[,2])


skogTemp_1 is probably a vector with only one dimension therefore you get
this error.
class(skogTemp_1)

shall give you the desired result, however I prefer

?str

Regards
Petr

> Error in skogTemp_1[, 2] : incorrect number of dimensions
> > class(skogTemp_1[1,2])
> Error in skogTemp_1[1, 2] : incorrect number of dimensions
>
> frustrating... I don't know what this mean.
>
> Can anyone help?
>
> Thank you,
> Angelica
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/converting-
>
factor-to-numeric-gives-NAs-introduced-by-coercion-tp3703408p3703408.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Odp: converting factor to numeric gives "NAs introduced by coercion"

jholtman
If you are not going to be using factors, then you can keep everything
a character (if there are non-numerics in a column) by adding
'as.is=TRUE' as a parameter on the 'rad.table' functions.

On Mon, Aug 1, 2011 at 7:55 AM, Petr PIKAL <[hidden email]> wrote:

> Hi
>
>> Hi,
>>
>> I have a dataframe that I imported from a .txt file by:
>>
>> skogTemp <- read.delim2("Skogaryd_shoot_data.txt", header=TRUE,
> fill=TRUE)
>>
>> and the data are factors, how can avoid factors from the beginning?
> Although
>> the file contains both characters and numbers.
>
> You have got an answer but here are some comments. If you have characters
> and numbers in one column the character values are converted to NA by
> as.numeric
>
>>
>> I tried to convert some of the columns from factor to numeric and as I
>> understood it you can not use only as.numeric but as.character first. I
> got
>> this warning message:
>>
>> > skogTemp_1 <- as.numeric(as.character(skogTemp_1[,2:4]))
>> Warning message:
>> NAs introduced by coercion
>
> What is skogTemp_1? I presume skogTemp is data frame and in that case you
> can not use such construction directly.
>
>>
>> I have lots of NAs in my data. Tries to check what class I had now but
>> another warning is given me:
>> > class(skogTemp_1[,2])
>
>
> skogTemp_1 is probably a vector with only one dimension therefore you get
> this error.
> class(skogTemp_1)
>
> shall give you the desired result, however I prefer
>
> ?str
>
> Regards
> Petr
>
>> Error in skogTemp_1[, 2] : incorrect number of dimensions
>> > class(skogTemp_1[1,2])
>> Error in skogTemp_1[1, 2] : incorrect number of dimensions
>>
>> frustrating... I don't know what this mean.
>>
>> Can anyone help?
>>
>> Thank you,
>> Angelica
>>
>>
>>
>> --
>> View this message in context: http://r.789695.n4.nabble.com/converting-
>>
> factor-to-numeric-gives-NAs-introduced-by-coercion-tp3703408p3703408.html
>> Sent from the R help mailing list archive at Nabble.com.
>>
>> ______________________________________________
>> [hidden email] 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.
>
> ______________________________________________
> [hidden email] 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.
>



--
Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?

______________________________________________
[hidden email] 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.