Need Help with Manipulating Columns

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

Need Help with Manipulating Columns

cavalier33901
I have uploaded the property data from Lee County Property Appraiser into R, but my problem is that I am unable manipulate and use many of the columns because they are classified as factors.  I think this is because the values represent selling prices and therefore have $ in front of them.  I tried using as.integer but this funtion converts the values into a totaly useless form.  Is there a way to convert these columns from factors into something usefull for plotting and graphing such as integer form? Thanks

Will
Reply | Threaded
Open this post in threaded view
|

Re: Need Help with Manipulating Columns

Erik Iverson-3
possible hint:

as.numeric(gsub("\\$", "", c("$1.23", "$1.32")))

cavalier33901 wrote:
> I have uploaded the property data from Lee County Property Appraiser into R,
> but my problem is that I am unable manipulate and use many of the columns
> because they are classified as factors.  I think this is because the values
> represent selling prices and therefore have $ in front of them.  I tried
> using as.integer but this funtion converts the values into a totaly useless
> form.  Is there a way to convert these columns from factors into something
> usefull for plotting and graphing such as integer form? Thanks
>
> Will

______________________________________________
[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: Need Help with Manipulating Columns

Duncan Murdoch
In reply to this post by cavalier33901
On 06/04/2010 3:40 PM, cavalier33901 wrote:
> I have uploaded the property data from Lee County Property Appraiser into R,
> but my problem is that I am unable manipulate and use many of the columns
> because they are classified as factors.  I think this is because the values
> represent selling prices and therefore have $ in front of them.  I tried
> using as.integer but this funtion converts the values into a totaly useless
> form.  Is there a way to convert these columns from factors into something
> usefull for plotting and graphing such as integer form? Thanks
Use stringsAsFactors=FALSE when you read the data, and they'll be left
as strings, not factors.  Then convert them by stripping off the
non-numeric bits and using as.numeric.

For example:

 > price <- " $123,456"
 > price <- gsub(",", "", price)   # remove comma
 > price <- gsub("^[[:blank:]]*", "", price) # remove leading whitespace
 > price <- gsub("\\$", "", price) # remove dollar sign
 >
 > price
[1] 123456

Duncan Murdoch

______________________________________________
[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: Need Help with Manipulating Columns

Erik Iverson-3
In reply to this post by Erik Iverson-3


Erik Iverson wrote:
> possible hint:
>
> as.numeric(gsub("\\$", "", c("$1.23", "$1.32")))

and convert to character first before as.numeric if they were factors to
begin with...


>
> cavalier33901 wrote:
>> I have uploaded the property data from Lee County Property Appraiser
>> into R,
>> but my problem is that I am unable manipulate and use many of the columns
>> because they are classified as factors.  I think this is because the
>> values
>> represent selling prices and therefore have $ in front of them.  I tried
>> using as.integer but this funtion converts the values into a totaly
>> useless
>> form.  Is there a way to convert these columns from factors into
>> something
>> usefull for plotting and graphing such as integer form? Thanks
>> Will
>
> ______________________________________________
> [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: Need Help with Manipulating Columns

cavalier33901
This post has NOT been accepted by the mailing list yet.
In reply to this post by Erik Iverson-3
Thanks I tried it with your values and it works, but when I try it with my values, plugging in "just" (not sure if i'm doing it right):    

> as.numeric(gsub("\\$", "", ("just")))

This is what I get:

[1] NA
Warning message:
NAs introduced by coercion

Btw here are the values im wokring with under "just"

 [1] $23,060  $124,800 $125,000 $28,140  $24,000  $23,400  $24,000  $149,720 $92,000  $35,000  $35,430  $40,100  $10,000  $25,000
Levels: $10,000 $124,800 $125,000 $149,720 $23,060 $23,400 $24,000 $25,000 $28,140 $35,000 $35,430 $40,100 $92,000
Reply | Threaded
Open this post in threaded view
|

Re: Need Help with Manipulating Columns

cavalier33901
In reply to this post by Duncan Murdoch
Nice, thanks A lot. I got it figured out