[英]How to convert a date with only a year to a date with the format “Year-Month-Day” in R
Sorry for the question, I started using RStudio a month ago and I get confronted to things I've never learned. 对于这个问题,很抱歉,我一个月前开始使用RStudio,但遇到了从未学过的事情。 I checked all the websites, helps and forums possible the past two days and this is getting me crazy.
在过去的两天里,我检查了所有可能的网站,帮助和论坛,这让我发疯。
I got a variable called Release
giving the date of the release of a song. 我有一个名为
Release
的变量,它给出了歌曲发行的日期。 Some dates are following the format %Y-%m-%d
whereas some others only give me a Year. 有些日期采用
%Y-%m-%d
格式,而另一些仅给我年份。 I'd like them to be all the same but I'm struggling to only modify the observations with the year. 我希望它们都一样,但是我努力只修改年份的观测值。
Brief summary in word: 简要总结:
11/11/2011
01/06/2011
1974
1970
16/09/2003
I've imported the data with : 我已使用导入数据:
music<-read.csv("music2.csv", header=TRUE, sep = ",", encoding = "UTF-8",stringsAsFactors = F)
And this how I have it in RStudio 这就是我在RStudio中的方式
"2011-11-11" "2011-06-01" "1974" "1970" "2003-09-16"
This is an example as I got 2200 obs. 这是一个示例,因为我有2200个Obs。
The working code is 工作代码是
Modifdates<- ifelse(nchar(music$Release)==4,paste0("01-01-",music$Release),music$Release)
Modifdates
I obtain this : 我得到这个:
"2011-11-11" "2011-06-01" "01-01-1974" "01-01-1970" "2003-09-16"
I just would like them to be all with the same format "%Y-%m-%d". 我只是希望它们都具有相同的格式“%Y-%m-%d”。 How can I do that?
我怎样才能做到这一点?
So I tried this 所以我尝试了这个
as.Date(music$Release,format="%Y-%m-%d")
But I got NA's where I modified my dates. 但是我得到了NA修改日期的地方。
Could anyone help? 有人可以帮忙吗?
Welcome to SO, please try to provide a reproducible example next time so that we can best help you. 欢迎使用SO,请下次尝试提供可复制的示例,以便我们为您提供最佳帮助。 I think here you could use:
我认为您可以在这里使用:
testdates <- c("1974", "12-12-2012")
betterdates <- ifelse(nchar(testdates)==4,paste0("01-01-",testdates),testdates)
> betterdates
[1] "01-01-1974" "12-12-2012"
EDIT: if your vector is factor you should use as.character.factor
first. 编辑:如果您的向量是因素,您应该首先使用
as.character.factor
。 If you then want to convert back to factor you can use as.factor
如果您随后想要转换回因子,则可以使用
as.factor
EDIT2 : do not convert as.date
before doing this. EDIT2:在执行此
as.date
之前,请不要转换as.date
。 Only do it after this modification 仅在此修改后执行
Update 更新资料
Using sub
find occurrences of date consisting from single year ( "(^[0-9]{4}$)"
part), using back-reference substitute it to add -01-01
at the end of the string ( "\\\\1-01-01"
part), and finally convert it to the date
class, using as.Date()
( as.Date()
default is format = "%Y-%m-%d"
so you don't need to specify it): 使用
sub
查找从单个年份( "(^[0-9]{4}$)"
部分)组成的日期,使用后向引用替换它,以在字符串的末尾添加-01-01
( "\\\\1-01-01"
部分),最后使用as.Date()
将其转换为date
类( as.Date()
默认为format = "%Y-%m-%d"
因此您不需要指定它):
dat <- c("2011-11-11", "2011-06-01", "1974", "1970", "2003-09-16")
dat
class is character
:
dat
类是character
:
as.Date(sub("(^[0-9]{4}$)", "\\1-01-01", dat))
# "2011-11-11" "2011-06-01" "1974-01-01" "1970-01-01" "2003-09-16"
dat
class is factor
, but sub
automatically coerce it to the character
class for you:
dat
类是factor
,但是sub
自动将character
强制转换为character
类:
# dat <- as.factor(dat); dat # 2011-11-11 2011-06-01 1974 1970 2003-09-16 # Levels: 1970 1974 2003-09-16 2011-06-01 2011-11-11 as.Date(sub("(^[0-9]{4}$)", "\\\\1-01-01", dat)) # "2011-11-11" "2011-06-01" "1974-01-01" "1970-01-01" "2003-09-16"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.