几个月没有前导零R [英] No leading zeros for months R

查看:108
本文介绍了几个月没有前导零R的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

format()没有明显的选项来显示不带前导0的月份(与年份相同).还有另一种方法可以得到这个结果吗?该解决方案应允许用户灵活选择是仅在一天,月份,年份还是任意组合中省略0.

format() in R does not have an obvious option to display the month without leading 0 (and the same with the year). Is there another way to get this result? The solution should allow the user to flexibly choose whether 0s are to be omitted only for the day or the month or the year or for any combination.

在:as.Date("2005-09-02")

输出:2/9/5

或仅删除一个月的0:

输出:2/9/05

推荐答案

使用sub的解决方案:

x <- as.Date("2005-09-02")
sub("..0?(.+)-0?(.+)-0?(.+)", "\\3/\\2/\\1", x)
# [1] "2/9/5"

这篇关于几个月没有前导零R的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆