Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Converting date columns from character to dates give me NAs as result

Im trying to convert my column names from character to dates but I get NAs instead

data2<-structure(list(Type = c("Spend", "Orders", "Revenue", "Impressions", 
"Clicks"), `45292...3` = c("28553.05", "492", "35221.050000000003", 
"4977289", "20570"), `45293...4` = c("30068.25", "478", "30472.83", 
"5405811", "24283"), `45294...5` = c("27220.44", "442", "26138.42", 
"4692552", "20689"), `45295...6` = c("26637.33", "400", "22904.21", 
"4079760", "17514")), row.names = c(NA, -5L), class = c("tbl_df", 
"tbl", "data.frame"))


date_columns <- colnames(data2)[2:ncol(data2)] |> 
  as.numeric() |> 
  as.Date(origin = "1900-01-01") |> 
  as.character()

colnames(data2) <- c(colnames(data2)[1],  date_columns) 

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Your column names have multiple ... in them, so remove them first:

colnames(data2)[2:ncol(data2)] <- 
  sub("\\.\\.", "", colnames(data2)[2:ncol(data2)]) |> 
  as.numeric() |> 
  as.Date(origin = "1900-01-01") |> 
  as.character()

# [1] "2024-01-03" "2024-01-04" "2024-01-05" "2024-01-06"
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading