【问题标题】:Removing slashes from dates within a data.frame从 data.frame 中的日期中删除斜线
【发布时间】:2014-07-23 04:48:22
【问题描述】:

我在 x

我只想删除 back 正斜杠。任何帮助将不胜感激。

【问题讨论】:

  • 您的示例中只有正斜杠 - 没有反斜杠。

标签: string r date dataframe


【解决方案1】:

另一种方法是在R中使用as.Date/strftime和其他日期时间操作函数

# create a Date column (can do things with dates as dates now)
dat$Date <- as.Date(as.character(dat$dates),format = '%d/%m/%Y')
# create character column with format of your desire
dat$newDate <- strftime(dat$Date, '%d%m%Y')

【讨论】:

    【解决方案2】:

    你不需要转义正斜杠(这不是你正在做的事情),所以这会起作用,例如:

    dat <- data.frame(dates=c("01/01/2009","02/01/2009"))
    dat
    #       dates
    #1 01/01/2009
    #2 02/01/2009
    
    dat$dates <- gsub("/","",dat$dates)
    dat
    #     dates
    #1 01012009
    #2 02012009
    

    【讨论】:

    • 谢谢!我想将此应用于存储为 data.frame 而不是单个输入的整个向量。有没有办法修改它来做到这一点?
    • @user2832896 - gsub 在单个值上的工作方式与在 data.frame 中的向量上的工作方式相同。查看我的编辑。
    猜你喜欢
    • 1970-01-01
    • 2021-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-20
    • 2011-01-16
    • 2012-07-02
    • 2015-06-06
    相关资源
    最近更新 更多