I'm using RStudio. My data frame contains a date-time stamp which I want to simplify for further use. I couldn't find a proper answer in the archive, yet.
This is how my data looks like:
df <- read.table("datafile.csv", skip = 9, dec = ".", sep = ",", stringsAsFactors = F)
names(df) <- df[1,]
df <- df[-c(1),]
head(df)
Time 10cm 20cm 30cm 40cm 50cm 60cm 70cm 80cm
2 05 Apr 2019 09:46:13 20.706751 26.204191 23.663700 18.041510 3.507654 5.644918 3.947458 0.926415
3 11 Apr 2019 08:36:32 18.457157 25.762735 23.822021 18.596792 3.829793 6.639636 4.313009 1.002555
4 19 Apr 2019 09:24:16 17.224855 24.033939 21.703968 16.956991 3.507654 6.827912 4.417910 1.046471
5 26 Apr 2019 12:14:05 16.603245 22.160437 19.541498 15.735872 3.237127 6.169124 3.987147 1.002555
Time is displayed as character. Now I wanted to get rid of the time stamp (from "%d.%m.%Y %H:%M:%S" to "%Y-%m-%d") by using one of the following commands.
(1)
df$Time <- format(as.POSIXct(strptime(df$Time,"%d.%m.%Y %H:%M:%S",tz="")) ,format = "%Y-%m-%d") head(df)
Time 10cm 20cm 30cm 40cm 50cm 60cm 70cm 80cm 90cm
2 <NA> 20.706751 26.204191 23.663700 18.041510 3.507654 5.644918 3.947458 0.926415 1.304021
3 <NA> 18.457157 25.762735 23.822021 18.596792 3.829793 6.639636 4.313009 1.002555 1.440603
4 <NA> 17.224855 24.033939 21.703968 16.956991 3.507654 6.827912 4.417910 1.046471 1.574125
5 <NA> 16.603245 22.160437 19.541498 15.735872 3.237127 6.169124 3.987147 1.002555 1.397690
(2)
df$Time = as.Date((df$Time), format = "%Y-%m-%d") head(df)
Time 10cm 20cm 30cm 40cm 50cm 60cm 70cm 80cm 90cm
2 <NA> 20.706751 26.204191 23.663700 18.041510 3.507654 5.644918 3.947458 0.926415 1.304021
3 <NA> 18.457157 25.762735 23.822021 18.596792 3.829793 6.639636 4.313009 1.002555 1.440603
4 <NA> 17.224855 24.033939 21.703968 16.956991 3.507654 6.827912 4.417910 1.046471 1.574125
5 <NA> 16.603245 22.160437 19.541498 15.735872 3.237127 6.169124 3.987147 1.002555 1.397690
(3)
format(as.Date(strptime(df$Time, "%d.%m.%Y %H:%M:%S")), "%Y-%m-%d")
[1] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
...but as you can see I only receive NA for each of them.
Where's my mistake? If anyone has some advice for coding AND an explanation I would appreciate that.