仅将R中的数值转换为日期字符串

时间:2019-01-29 03:54:06

标签: r date

假设我有一个在数字和字符串之间混合的值向量,如下所示:

df<-structure(c("Location", "SKU", "Manufacturer", "Size", 
            "State", "43488", "43489", "43490", 
            "43491"), .Names = c("col1","col2","col3","col4","col5","col6","col7","col8","col9"))

我只想将数字值(最初是Excel日期)转换为R日期格式,而其余变量保持原样。我正在使用的向量在较大的data.frame中只是一行,所以我宁愿避免将日期从字符串中拆分并随后合并。 到目前为止,我尝试了以下方法:

as.Date(as.numeric(df), origin = "1899-12-30")
[1] NA           NA           NA           NA           NA           "2019-01-23" "2019-01-24" "2019-01-25" "2019-01-26"

但是这会将字符串转换为NA。

2 个答案:

答案 0 :(得分:2)

我们可以找出其中包含数字的索引,然后仅将这些值转换为日期。

DB::select("SELECT * FROM my_table WHERE vendor_id IN (" . implode(array_keys($vendors), ',') . ")", 
    $vendors);

使用inds <- grepl("\\d+", df) df[inds] <- format(as.Date(as.numeric(df[inds]), origin = "1899-12-30")) df # col1 col2 col3 col4 col5 #"Location" "SKU" "Manufacturer" "Size" "State" # col6 col7 col8 col9 #"2019-01-23" "2019-01-24" "2019-01-25" "2019-01-26" 对上述答案的一种变体,而没有创建其他变量(ifelse

inds

答案 1 :(得分:1)

尝试一下:
-创建一个函数来检查值是否可以强制为numeric类型
  -如果为true,则将其转换为numeric并将其格式化为date
  -如果为false,则按原样返回值

df<-structure(c("Location", "SKU", "Manufacturer", "Size", 
                "State", "43488", "43489", "43490", 
                "43491"), .Names = c("col1","col2","col3","col4","col5","col6","col7","col8","col9"))

convert_num_to_date = function(x){
  if (is.na(as.numeric(x))) {
    return(x)
  } else {
    x = format(as.Date(as.numeric(x), origin = "1899-12-30"))
    return(x)
  }
}

df = sapply(df, convert_num_to_date)