※ 引述《clansoda (小笨)》之銘言:
: 我目前有一個dataframe,我想把裡面其中一個coloum從character轉換成date
: 但是我只要一轉,他就會全部變成NA,我把locale調成LC_TIME C也沒有作用
: 想請問有人碰過同樣的情形或者有解法嗎?
: 這是轉換後的warrning
: 補上範例檔案,因為是商用檔案所以把個資都刪除了
: https://drive.google.com/open?id=0B3xY9FMzjybzSUt5TTNOWERicmM
: 語法是 as.Date(訂單日期)->xxd$訂單日期
: Warning message:
: In grepl("|||", what, fixed = TRUE) :
: input string 1 is invalid in this locale
: 這是我的session info
: R version 3.2.5 (2016-04-14)
: Platform: x86_64-w64-mingw32/x64 (64-bit)
: Running under: Windows >= 8 x64 (build 9200)
: locale:
: [1] LC_COLLATE=Chinese (Traditional)_Taiwan.950 LC_CTYPE=Chinese
: (Traditional)_Taiwan.950 LC_MONETARY=Chinese (Traditional)_Taiwan.950
: [4] LC_NUMERIC=C
: LC_TIME=C
: attached base packages:
: [1] stats graphics grDevices utils datasets methods base
: other attached packages:
: [1] tidyr_0.4.1 recommenderlab_0.1-9 registry_0.3
: proxy_0.4-15 arules_1.4-1 Matrix_1.2-6
: gtools_3.5.0
: [8] dplyr_0.4.3 plyr_1.8.3
: loaded via a namespace (and not attached):
: [1] Rcpp_0.12.5 lattice_0.20-33 assertthat_0.1 grid_3.2.5
: R6_2.1.2 DBI_0.4-1 magrittr_1.5 irlba_2.0.0
: tools_3.2.5
: [10] parallel_3.2.5
我轉出來是沒問題,程式:
library(data.table)
library(dplyr)
dat = fread('ss.csv') %>% tbl_df %>%
mutate(訂單日期 = as.Date(gsub('-', '/', 訂單日期)))
# > str(dat)
# Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 11338 obs. of 173 variables:
# $ V1 : chr "1" "2" "3" "4" ...
# $ 訂單日期 : Date, format: "2016-04-01" "2016-04-01" ...
# $ 訂單狀態 : chr "已確認" "已完成" "已確認" "已確認" ...
我合理懷疑你的訂單日期type是factor才會轉失敗,character會成功
應該不會只是忘了加xxd$,像這樣:
as.Date(訂單日期)->xxd$訂單日期 => as.Date(xxd$訂單日期)->xxd$訂單日期
session information:
> sessionInfo()
R version 3.2.4 (2016-03-16)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1
locale:
[1] LC_COLLATE=Chinese (Traditional)_Taiwan.950 LC_CTYPE=Chinese
(Traditional)_Taiwan.950
[3] LC_MONETARY=Chinese (Traditional)_Taiwan.950
LC_NUMERIC=C
[5] LC_TIME=Chinese (Traditional)_Taiwan.950
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] dplyr_0.4.3.9001 fasttime_1.0-1 data.table_1.9.7
RevoUtilsMath_3.2.4
loaded via a namespace (and not attached):
[1] lazyeval_0.1.10.9000 magrittr_1.5 R6_2.1.2
assertthat_0.1.0.99 rsconnect_0.4.2.1
[6] DBI_0.3.1 tools_3.2.4 tibble_1.0-1
Rcpp_0.12.4.5