转换R中的一位数字天

时间:2018-11-13 19:09:38

标签: r date format

我的日期格式为Apr42016,Aug12017,Apr112018。我正在尝试使用R在Y / m / d中进行转换。我尝试了以下代码,但是当返回日期为NA时只有一位数字。有人可以帮助我吗?

strptime(data$date, "%b%e%Y")
as.Date (data$date, format="%b%d%Y")
as.POSIXct(data$date, format="%b%e%Y")

谢谢

2 个答案:

答案 0 :(得分:2)

如果您可以分解CREATE TABLE `bwc_master`( unique_id bigint, customer_id string, direction string, call_date_time timestamp, duration int, billed_duration int, retail_rate decimal(9,7), retail_cost decimal(19,7), billed_tier smallint, call_type tinyint, record_status tinyint, aggregate_id bigint, originating_ipaddress string, originating_number string, destination_number string, lrn string, ocn string, destination_rate_center string, destination_lata int, billed_prefix string, rate_id string, wholesale_rate decimal(9,7), wholesale_cost decimal(19,7), cnam_dipped boolean, billed_number_type tinyint, source_lata int, source_ocn string, location_id string, sippeer_id int, rate_attempts tinyint, source_state string, source_rc string, destination_country string, destination_state string, destination_ip string, carrier_id string, rated_date_time timestamp, partition_id smallint, encryption_rate decimal(9,7), encryption_cost decimal(19,7), trans_coding_rate decimal(9,7), trans_coding_cost decimal(19,7), file_name string, call_id string, from_tag string, to_tag string, unique_record_id string) PARTITIONED BY ( `call_date` date) CLUSTERED BY ( customer_id) INTO 10 BUCKETS ROW FORMAT SERDE 'org.apache.hadoop.hive.ql.io.orc.OrcSerde' STORED AS INPUTFORMAT 'org.apache.hadoop.hive.ql.io.orc.OrcInputFormat' OUTPUTFORMAT 'org.apache.hadoop.hive.ql.io.orc.OrcOutputFormat' LOCATION 'hdfs://*****/apps/hive/warehouse/dtc.db/bwc_master' 之前的数字,则将使事情变得容易得多。 (借用Sven的回头率。)

as.Date

从这里开始,格式应该相当简单:

sub("(?<=\\D)(\\d+)(\\d{4})$", "-\\1-\\2",
  c("Apr42016", "Aug12017", "Apr112018"), perl=TRUE)
# [1] "Apr-4-2016"  "Aug-1-2017"  "Apr-11-2018"

答案 1 :(得分:2)

在使用df %>% mutate_at(vars(life_expectancy, poverty, household_income), function(x) map(x, round, digits = c(1, 2, 0))) %>% unnest() #> # A tibble: 3 x 4 #> name life_expectancy poverty household_income #> <chr> <dbl> <dbl> <dbl> #> 1 New Haven 78.1 0.3 42589. #> 2 New Haven 78.1 0.26 42589. #> 3 New Haven 78 0 42589 之前,您可以使用sub修改字符串(并在必要时添加0)。

as.Date