Back

Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
in R Programming by (50.2k points)

I have the following data table and from that, I just want to add 01 in the dt_start column:

library(data.table)

DT <- data.table(n = c("A", "B"), dt_start = c("10/2020", "05/2015"))

I want 

```r

DT <- data.table(n = c("A", "B"), dt_start = c("2020-10-01", "2015-05-01"))

1 Answer

0 votes
by (108k points)

For adding the values in the pre-existing data table, you can just apply the paste0 to add the day to the character and then use the as.Date to control it:

DT[, dt_start := as.Date(paste0("01/", DT$dt_start), "%d/%m/%Y")]

The result of the above command in R programming will be:

DT

   n   dt_start

1: A 2020-10-01

2: B 2015-05-01

Browse Categories

...