I am trying to read an R csv file containing information about political contributions. From what I understand, columns are imported by default as factors, but I need the quantity column ('CTRIB_AMT' in the dataset) to be imported as a numeric column, so I can perform various functions that factors will not work. The column is formatted as a currency with the prefix "$".
I used a simple read command to initially import the file:
contribs <- read.csv('path/to/file')
And then I tried to convert CTRIB_AMT from currency to numeric:
as.numeric(as.character(sub("$","",contribs$CTRIB_AMT, fixed=TRUE)))
But that did not work. The functions I'm trying to use for CTRIB_AMT columns are as follows:
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 the file initially so that the column is numeric or how to convert it after import?
r
tchaymore
source share