mhab
October 3, 2022, 2:07pm
1
Hi,
Can anyone help me. I want create date column from column a. But, there is letter X in the column.
How to remove the letter X and convert into date format. Below were the example. Thank you for your helps
a b
1 X2010.01.01 | 0.009397952
2 X2010.02.01 | 0.006056374
3 X2010.03.01 | 0.004757837
4 X2010.04.01 | 0.006765032
FJCC
October 3, 2022, 2:42pm
2
Here is an example. The str_remove() function deletes the X character and the ymd function converts the remaining text to a date.
DF <- data.frame(Date=c("X2010.01.01","X2010.02.01", "X2010.03.01", "X2010.04.01"),
Value = c( 0.009397952, 0.006056374, 0.004757837, 0.006765032))
DF
#> Date Value
#> 1 X2010.01.01 0.009397952
#> 2 X2010.02.01 0.006056374
#> 3 X2010.03.01 0.004757837
#> 4 X2010.04.01 0.006765032
library(stringr)
library(lubridate)
library(dplyr)
DF <- DF |> mutate(Date = ymd(str_remove(Date,"X")))
DF
#> Date Value
#> 1 2010-01-01 0.009397952
#> 2 2010-02-01 0.006056374
#> 3 2010-03-01 0.004757837
#> 4 2010-04-01 0.006765032
Created on 2022-10-03 with reprex v2.0.2
1 Like
mhab
October 3, 2022, 2:55pm
3
Thank you very much.
That really help..
system
Closed
October 24, 2022, 2:57pm
4
This topic was automatically closed 21 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.