假設建立一個數值型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.
Popular Posts
Categories
R
(28)
data.table
(4)
Python
(3)
Rstudio
(3)
dplyr
(3)
rvest
(3)
網路爬蟲
(3)
Error
(2)
Web Crawler
(2)
grepl
(2)
jupyter
(2)
plyr
(2)
ubuntu
(2)
教學
(2)
.Last.value
(1)
Big Data
(1)
Console
(1)
IEEE程式語言排行
(1)
PuTTY
(1)
Rprofile.site
(1)
Rselenium
(1)
XLConnect
(1)
assign
(1)
bar chart
(1)
cat
(1)
conflict
(1)
coord_flip
(1)
data.frame
(1)
dcast
(1)
download.file
(1)
evalWithTimeout
(1)
excel_sheets
(1)
factor
(1)
file.rename
(1)
fread
(1)
ggplot2
(1)
global variable
(1)
group_by
(1)
gsub
(1)
invalid multibyte character
(1)
jiebaR
(1)
join
(1)
jupyter_contrib_nbextensions
(1)
jupyterthemes
(1)
loading
(1)
melt
(1)
merge
(1)
mutate
(1)
numeric
(1)
print
(1)
rbind
(1)
read.csv
(1)
read_csv
(1)
read_excel
(1)
readr
(1)
readxl
(1)
scientific notation
(1)
scipen
(1)
separate_rows
(1)
setDF
(1)
setDT
(1)
sqldf
(1)
static IP address
(1)
str_count
(1)
stringr
(1)
table
(1)
tidyr
(1)
timeout
(1)
trim
(1)
txtProgressBar
(1)
unique
(1)
zip
(1)
人力銀行
(1)
參考資源
(1)
技能
(1)
文字探勘
(1)
橫條圖
(1)
玩玩小數據
(1)
結巴分詞
(1)
能力
(1)
資料分析
(1)
資料分析師
(1)
長條圖
(1)
沒有留言:
張貼留言