1

列が に設定されているデータベースから数値データを抽出しようとしていますVARCHAR(100)。関連する列のすべてのデータは数値であるため、整数としてフォーマットされたデータを抽出しても問題はありません。Rでこれを行う良い方法はありますか?

これが私が得たものです:

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=""))

以下を実行します。

sum(m_df$Power)

次のエラーが発生します。

Error in sum(m_df$Power) : invalid 'type' (character) of argument

実行:

str(m_df)

生成:

 'data.frame':   4317 obs. of  2 variables:
 $ Position: chr  "280" "281" "288" "294" ...
 $ Power   : chr  "294" "342" "324" "284" ...
4

1 に答える 1

3

You are trying to sum some characters and R is saying "WHAAAA?". The little snippet that follows reproduces your error.

(x.char <- sum(c("1", "2", "3")))
 Error in sum(c("1", "2", "3")) : invalid 'type' (character) of argument
(sum(as.numeric(x.char)))
 [1] 6

Run as.numeric function on your data.frame and you're good to go.

于 2011-07-21T10:07:45.133 に答える