Re: [問題] 格式轉換

看板R_Language作者 (天)時間8年前 (2016/06/06 12:33), 8年前編輯推噓2(203)
留言5則, 2人參與, 最新討論串2/2 (看更多)
※ 引述《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 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 140.109.74.87 ※ 文章網址: https://www.ptt.cc/bbs/R_Language/M.1465187597.A.51A.html

06/06 13:06, , 1F
C大,你可以往下拉看看有沒有NA嗎?
06/06 13:06, 1F

06/06 13:06, , 2F
我的是可以轉,可是下面有一部分的資料會變成NA
06/06 13:06, 2F
因為你的日期格式有些是-分隔有些是/,做一個取代的動作把-都換成/就會正常了 上面程式已更動。 ※ 編輯: celestialgod (140.109.74.87), 06/06/2016 13:11:13

06/06 13:36, , 3F
感謝C大,我自己都沒發現,真的分隔符號出了問題
06/06 13:36, 3F

06/06 14:02, , 4F
型態轉換,通常建議做一下unique看一下字串的長相,
06/06 14:02, 4F

06/06 14:02, , 5F
避免奇怪的現象出現
06/06 14:02, 5F
文章代碼(AID): #1NLFqDKQ (R_Language)
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 2 之 2 篇):
文章代碼(AID): #1NLFqDKQ (R_Language)