I want to change te date but it says my input string is too long

In my dataset all the dates are written like this 20150101, 20150102, 20150103. I want to change that to %m-%d-%Y but it says my input string is too long. Does anybody know how?

library(tidyverse)
library(dplyr)
library(stringr)
library(plyr)
library(ggplot2)
library(tidyr)

wind <- read_csv("KNMI wind.csv")

glimpse(wind_schoon)

wind_schoon <- separate(wind, col = 1,
                        into = c("Station", "Date", "Windsnelheid (in 0,1 m/s)"),
                        sep = ";")
wind_schoon[is.na(wind_schoon)] <- 0
#Wind data omzetten
wind_schoon$`Windsnelheid (in 0,1 m/s)` <- as.integer(as.character(wind_schoon$`Windsnelheid (in 0,1 m/s)`))
wind_schoon$Station <- as.numeric(as.character(wind_schoon$Station))
wind_schoon$Date <- strptime(wind_schoon, format = "%m-%d-%Y")

after this step please do

dput(head(wind_schoon,10))

and share the results.
This would make a reprex,
reprex

structure(list(Station = c(NA, 209, 209, 209, 209, 209, 209, 
209, 209, 209), Date = c("", "20150101", "20150102", "20150103", 
"20150104", "20150105", "20150106", "20150107", "20150108", "20150109"
), `Windsnelheid (in 0,1 m/s)` = c(NA, 133L, 144L, 95L, 77L, 
78L, 67L, 99L, 112L, 143L)), row.names = c(NA, -10L), class = c("tbl_df", 
"tbl", "data.frame"))
w <- structure(list(Station = c(NA, 209, 209, 209, 209, 209, 209, 
                           209, 209, 209), Date = c("", "20150101", "20150102", "20150103", 
                                                    "20150104", "20150105", "20150106", "20150107", "20150108", "20150109"
                           ), `Windsnelheid (in 0,1 m/s)` = c(NA, 133L, 144L, 95L, 77L, 
                                                              78L, 67L, 99L, 112L, 143L)), row.names = c(NA, -10L), class = c("tbl_df", 
                                                                                                                              "tbl", "data.frame"))

w[is.na(w)] <- 0
#Wind data omzetten

w$Date <- strptime(w$Date, format = "%Y%m%d")
w$Date_txt_formatted <- as.character.Date(w$Date, format = "%m-%d-%Y")

#if you want rid of rows with NA
w2 <- na.omit(w)

the principle to follow is that when you want to present to the user a customised view of a date field, to render that field as text with the required formatting applied, keep the true date field around to calculate on, you don't have to show it to the users if you dont want to.

thanks for helping me! But now al my 0's have turned back to NA's and i need them to be 0's again. How is that possible?

again, you can't have a zero dttm, but you can format your text date field.

w$Date_txt_formatted <- ifelse(!is.na(w$Date),
                               as.character.Date(w$Date, format = "%m-%d-%Y"),
                               0)

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.