I'm trying to read into R a csv file that contains information on political contributions. From what I understand, the columns by default are imported as factors, but I need the the amount column ('CTRIB_AMT' in the dataset) to be imported as a numeric column so I can run a variety of functions that wouldn't work for factors. The column is formatted as a currency with a "$" as prefix.
I used a simple read command to import the file initially:
contribs <- read.csv('path/to/file')
And then tried to convert the CTRIB_AMT from currency to numeric:
as.numeric(as.character(sub("$","",contribs$CTRIB_AMT, fixed=TRUE)))
But that didn't work. The functions I'm trying to use for the CTRIB_AMT columns are:
vals<-sort(unique(dfr$CTRIB_AMT))
sums<-tapply( dfr$CTRIB_AMT, dfr$CTRIB_AMT, sum)
counts<-tapply( dfr$CTRIB_AMT, dfr$CTRIB_AMT, length)
See related question here.
Any thoughts on how to import file initially so column is numeric or how to convert it after importing?
sub
->as.character
->as.numeric
conversion should do the trick. What do you mean by "didn't work"? – ClavusCTRIB_AMT <- factor(c("$5000","$2500","$100")); as.numeric(as.character(sub("$","",CTRIB_AMT, fixed=TRUE)))
– Aforementioned