[R] 當數值型轉Factor再轉回數字型資料時需注意之事項

假設建立一個數值型Vector,
當數值型資料轉成Factor後,
再轉回數值型態,
可能會遇到問題。

例如建立一個 tmp:tmp <- c(2,3,3,2,4,4)
將其轉成Factor: f_tmp <- factor(tmp)
當再把f_tmp轉回數值型態:n_tmp <- as.numeric(f_tmp)
會發現n_tmp並不是2 3 3 2 4 4,
而是 1 2 2 1 3 3,
這原因是當你將factor型態轉成數值型態,
是以其level順序來轉成數值型態(原始資料的2是第一個level,3是第二個level,4是第三個level)。
所以需要先將f_tmp轉成文字型態,
再轉成數值型態即可:n_c_tmp <- as.numeric(as.character(f_tmp))
下方有小範例。

When you want to change a factor full of numeric data to numeric,
you have to be aware that when you only use as.numeric on the factor,
it will return to you the levels of the factor,
not the original value.
So first you should change the factor to character,
and then change it to numeric.
It will return to you the original value of the factor.
The example is as follows.

沒有留言:

張貼留言