【问题标题】:Convert a date column to a 'period-year' column将日期列转换为“期间-年”列
【发布时间】:2015-06-17 17:53:15
【问题描述】:

我有这个包含 4000 次观察的数据,所以这是head(both):

     kön             gdk   age fbkurs     pers   stterm
1    man          FALSE    69  FALSE 1941-12-23 2011-01-19
2    man             NA    70  FALSE 1942-02-11 2012-01-19
3 kvinna             NA    65  FALSE 1942-06-04 2007-09-01
4 kvinna           TRUE    68  FALSE 1943-04-04 2011-09-01
5 kvinna             NA    65  FALSE 1943-10-30 2008-09-01
6    man          FALSE    70   TRUE 1944-01-27 2013-09-01

我想根据名为“stterm”的列创建一个新列。 例如,在 stterm 中,我宁愿命名不同的日期。 VT10、VT11等。我喜欢叫新专栏regyear。

我已尝试输入:

regyear <- factor(both$stterm, levels = c("2007-09-01"="HT07" "2008-09-01"="HT09" "2009-01-19"="VT09" "2009-09-01"="HT09" "2010-01-19"="VT10" "2010-09-01"="HT10" "2011-01-19"="VT11"
                                       "2011-09-01"="HT11" "2012-01-19"="VT12" "2012-09-01"="HT12" "2013-01-19"="VT13" "2013-09-01"="HT13" "2014-01-19"="VT14"))

但是当我这样做时,我收到以下错误消息:

Error: unexpected string constant in "regyear<- factor(both$stterm, levels = c("2007-09-01"='HT07' "2008-09-01""

我应该怎么做才能使它们正确?

【问题讨论】:

  • 你需要给factors两个向量,一个叫做levels levels=c("2007-09-01", "2008-09-01", ...),另一个叫做labels labels=c("HT07", "HT09", ...)。
  • 首先,您必须用逗号分隔标签(例如 c("2007-09-01"="HT07", "2008-09-01"="HT09" ... )。但无论如何,我注意到两个不同的日期被映射到同一个字符串(HT09)......那么,你的目标是简单地将一个或多个日期映射到另一个字符串吗?如果是这样,您不需要严格使用因子...

标签: r string date syntax-error


【解决方案1】:

你可以这样做:

both$regyear<- factor(both$stterm, labels = c("2007-09-01"="HT07","2008-09-01"="HT09",
                                              "2011-01-19"="VT11","2011-09-01"="HT11",
                                              "2012-01-19"="VT12","2013-09-01"="HT13"))

你原来的代码有几个问题:

  1. 它没有在您的数据框中创建新变量:regyear&lt;- factor(both$stterm, ... 应该是 both$regyear&lt;- factor(both$stterm, ...
  2. 级别/标签之间没有逗号。
  3. 对于给定的示例数据集,您必须有多个级别(请参阅这些instructions on how to give a reproducable example)。

【讨论】:

    【解决方案2】:

    您的代码依赖于相当多的硬编码,这可能容易出错,并且如果您有许多日期要映射到句点,这将是乏味的。

    这里有一些替代方法,您的日期首先使用as.Date 转换为Date 类。这使得提取月份并将其映射到“VT”或“HT”期间以及提取年份变得更加容易。

    在第一个示例中,我使用cut,它“将x 的范围划分为多个区间,并根据它们所在的区间对x 中的值进行编码。”:

    # some dates which are converted to proper R dates
    dates <- as.Date(c("2006-09-01", "2007-02-01", "2008-09-01", "2009-01-19"))
    
    # extract month
    month <- as.integer(format(dates, "%m"))
    
    # extract year
    year <- format(dates, "%y")
    
    # cut the months into intervals and label the levels
    term <- cut(x = month, breaks = c(0, 8, 12), labels = c("VT", "HT"))
    
    # paste 'term' and 'year' together
    paste0(term, year)
    # [1] "HT06" "VT07" "HT08" "VT09" 
    

    在第二个示例中,findInterval 用于创建区间索引的数字向量。该向量用于从“周期”向量中提取元素。然后将期间粘贴到上面的年份。

    paste0(c("VT", "HT")[findInterval(x = month, vec = c(1, 9))], year)
    # [1] "HT06" "VT07" "HT08" "VT09"
    

    最后,一种类似的、更“手动”的方法,如果您有许多“休息时间”和要映射日期的间隔,则不太方便:

    paste0(c("VT", "HT")[as.integer(month > 8) + 1], year)
    # [1] "HT06" "VT07" "HT08" "VT09"
    

    另一个相关问答here。

    【讨论】:

    • 不错的答案:比我的更普遍
    猜你喜欢
    • 1970-01-01
    • 2020-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-20
    • 1970-01-01
    相关资源
    最近更新 更多