integer data frame to date in R -
i have data frame 10 dates read r integers. here data frame:
19820509 19550503 20080505 19590505 19940517 19690504 20050420 20060503 19840427 19550513 we'll called df.
i have attempted few different lines of code here change each value date format in r this: "1982-05-09"
df <- as.date(df, "%y%m%d") doesn't work , neither
df <- as.posixlt(df, format = "%y/%m/%d") or
df <- as.posixct(df), format = "%y/%m/%d", origin = "19820509") i keep getting error saying "do not know how convert 'df' class "date" or either of posix formats.
i thought more simple. ideas?
thank you.
you have reference specific columns rather referencing data frame. if variable containing integer dates in data frame df , called x, can this:
df <- transform(df, x = as.date(as.character(x), "%y%m%d")) # x # 1 1982-05-09 # 2 1955-05-03 # 3 2008-05-05 # 4 1959-05-05 # 5 1994-05-17 # 6 1969-05-04 # 7 2005-04-20 # 8 2006-05-03 # 9 1984-04-27 # 10 1955-05-13 this converts integers character strings, interprets strings dates.
if multiple columns containing dates in format, can convert them in 1 fell swoop, have differently:
df <- data.frame(lapply(df, function(x) as.date(as.character(x), "%y%m%d"))) or better, docendo discimus mentioned in comment:
df[] <- lapply(df, function(x) as.date(as.character(x), "%y%m%d"))
Comments
Post a Comment