Re: [问题] 格式转换

楼主: celestialgod (天)   2016-06-06 12:33:14
※ 引述《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
作者: clansoda (小笨)   2016-06-06 13:06:00
C大,你可以往下拉看看有没有NA吗?我的是可以转,可是下面有一部分的资料会变成NA感谢C大,我自己都没发现,真的分隔符号出了问题
楼主: celestialgod (天)   2016-06-06 14:02:00
型态转换,通常建议做一下unique看一下字串的长相,避免奇怪的现象出现

Links booklink

Contact Us: admin [ a t ] ucptt.com