I am trying to extract numeric data from a database where the columns have been set as VARCHAR(100). All data in the relevant columns are numeric so there shouldn’t be any problem extracting the data formatted as integer. Is there a nice way to do this in R?
Here is what I got:
m_df <- dbGetQuery(conn, paste("SELECT ", direc, " as Position, ", power, " as Power FROM ", table,
" d LEFT JOIN files f on f.id=d.fileid WHERE parc='", parc,
"' AND timestamp >= '", w_date[1], "' and timestamp <= '", w_date[2],
"' AND plantnumber = ", w_mach, sep=""))
Executing the following:
sum(m_df$Power)
Produces this error:
Error in sum(m_df$Power) : invalid 'type' (character) of argument
Performing:
str(m_df)
Generates:
'data.frame': 4317 obs. of 2 variables:
$ Position: chr "280" "281" "288" "294" ...
$ Power : chr "294" "342" "324" "284" ...
You are trying to sum some characters and R is saying “WHAAAA?”. The little snippet that follows reproduces your error.
Run
as.numericfunction on your data.frame and you’re good to go.