无法将字符串转换为日期

时间:2014-01-31 21:02:31

标签: r date

这是我的代码:

   STUDYID COUNTRY SITEID SUBJID   SCRNDT    RANDDT   SCRNFDT TLSAMP SCRNSAMP
 GRTMD101     USA     13 130101 4-Dec-12            7-Dec-12    600     1224
 GRTMD101     USA     13 130102 4-Dec-12           26-Dec-12    600     1224
 GRTMD101     USA     13 130103 4-Dec-12           18-Dec-12    600     1224
 GRTMD101     USA      6  60101 5-Dec-12 18-Dec-12              600     1224
 GRTMD101     USA      5  50101 5-Dec-12           11-Dec-12    600     1224
 GRTMD101     USA     13 130104 6-Dec-12 26-Dec-12              600     1224

a<-read above dataset, sorry I don't know how to post this
subvars<-c("STUDYID","SITEID","SUBJID", "RANDDT")
b<-subset(a[subvars],a$RANDDT!="")
c<-as.Date(b$RANDDT,format="%d-%b-%y")

然而,R只是继续在NAs中给我所有c,没有任何东西可以转换。

我不知道为什么这项简单的任务无法完成。

你们有什么想法吗?

1 个答案:

答案 0 :(得分:0)

以下是转换RANDDT变量的方法:

library(lubridate)

a = structure(list(STUDYID = c("GRTMD101", "GRTMD101", "GRTMD101", 
"GRTMD101", "GRTMD101", "GRTMD101"), COUNTRY = c("USA", "USA", 
"USA", "USA", "USA", "USA"), SITEID = c(13L, 13L, 13L, 6L, 5L, 
13L), SUBJID = c(130101L, 130102L, 130103L, 60101L, 50101L, 130104L
), SCRNDT = c("4-Dec-12", "4-Dec-12", "4-Dec-12", "5-Dec-12", 
"5-Dec-12", "6-Dec-12"), RANDDT = c("", "", "", "18-Dec-12", 
"", "26-Dec-12"), SCRNFDT = c("7-Dec-12", "26-Dec-12", "18-Dec-12", 
"", "11-Dec-12", ""), TLSAMP = c(600L, 600L, 600L, 600L, 600L, 
600L), SCRNSAMP = c(1224L, 1224L, 1224L, 1224L, 1224L, 1224L)), .Names = c("STUDYID", 
"COUNTRY", "SITEID", "SUBJID", "SCRNDT", "RANDDT", "SCRNFDT", 
"TLSAMP", "SCRNSAMP"), class = "data.frame", row.names = c(NA, 
-6L))


b = a[ a$RANDDT != '', c(1,3,4,6)]

b$RANDDT = dmy(b$RANDDT)
b