[R] Need Help with Manipulating Columns

Duncan Murdoch murdoch at stats.uwo.ca
Tue Apr 6 22:10:16 CEST 2010


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



More information about the R-help mailing list