使用R将因子转换为日期

时间:2015-09-05 17:16:34

标签: r

我知道这个问题已被反复询问,道歉,但我无法看到错误...... 试图将一个因子转换为如下所示的日期,无论我尝试过什么,我仍然会得到N / A.

> class(cd$StartDate)
[1] "factor"
> head(cd)
  StartDate      Phase Cancer
1   Dec-89   Phase  2  breast
2   Jul-89   Phase  2  breast
3   Sep-92   Phase  1  breast

> cd$dates <- as.Date(cd$StartDate, format = "%b-%y)

> head(cd)
  StartDate      Phase Cancer dates
1   Dec-89   Phase  2  breast  <NA>
2   Jul-89   Phase  2  breast  <NA>
3   Sep-92   Phase  1  breast  <NA>

2 个答案:

答案 0 :(得分:3)

我认为您需要提供值才能使转换生效:

String lineNumberPattern = "(\\d+\\s)";
String timeStampPattern = "([\\d:,]+)";
String contentPattern = "(.*)";

// the complete regexp : "(\\d+\\s)([\\d:,]+)( --> )([\\d:,]+)(\\s)(.*)"

String sampleLine = "2\n00:00:02,373 --> 00:00:03,999\nOhh wooaah\n";
Matcher matcher = Pattern.compile(lineNumberPattern + timeStampPattern + "( --> )" + timeStampPattern + "(\\s)" + contentPattern).matcher(sampleLine);

while(matcher.find()) {
    String start = matcher.group(2);
    String end = matcher.group(4);
    String content = matcher.group(6);
    // store those information somewhere
}

数据:

cd$dates <- as.Date(
  paste0(as.character(cd$StartDate), "-01"), 
  format = "%b-%y-%d")
##
R> str(cd)
#'data.frame':  3 obs. of  4 variables:
#$ StartDate: Factor w/ 3 levels "Dec-89","Jul-89",..: 1 2 3
#$ Phase    : chr  "Phase_2" "Phase_2" "Phase_1"
#$ Cancer   : chr  "breast" "breast" "breast"
#$ dates    : Date, format: "1989-12-01" "1989-07-01" "1992-09-01"

答案 1 :(得分:0)

我也尝试过:

as.Date(as.character(cd$StartDate),format = "%m/%d/%y")

但低于一个

 a<-as.character(cd$StartDate)
 b<-as.Date.character(a, format = "%m/%d/%Y")
 b

格式中大写的Y有所不同。