[英]How to remove the day from the date and leave only the month and year from a column in a large dataset in r markdown?
I have a large dataset and one of the columns includes dates formatted DD/MM/YYYY and I would like to just have MM/YYYY.我有一个大型数据集,其中一列包括格式为 DD/MM/YYYY 的日期,我想只有 MM/YYYY。 Is there a way to apply this to the entire column in the dataset
dat$date
?有没有办法将此应用于数据集
dat$date
中的整个列?
We can convert to Date
class first with as.Date
and then use format
我们可以先用
as.Date
转换为Date
class 然后使用format
dat$date <- format(as.Date(dat$date, "%d/%m/%Y"), "%m/%Y")
Or another option is regex to match the digits ( \\d+
) from the start ( ^
) of the string followed by /
and replace with blank ( ''
)或者另一个选项是正则表达式来匹配从字符串的开头(
^
)开始的数字( \\d+
),然后是/
并替换为空白( ''
)
dat$date <- sub("^\\d+\\/", "", dat$date)
dat <- data.frame(date = c('05/10/2015', '15/05/2010'), stringsAsFactors = FALSE)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.