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

仅将R中数值的字符串转换为日期,r,date,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")) as.Date(as.nume

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

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

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

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" 
试试这个:
-创建一个函数来检查值是否可以强制为
数值类型
类型
-如果
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)

dft此数据应该在上游进行更好的排序,这样就不会出现这种情况。@alistaire如果可以的话!:(
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)