當數值型資料轉成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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
tmp <- c(2,3,3,2,4,4) | |
f_tmp <- factor(tmp) | |
n_tmp <- as.numeric(f_tmp) | |
n_tmp | |
n_c_tmp <- as.numeric(as.character(f_tmp)) | |
n_c_tmp |
沒有留言:
張貼留言