bibalabo bibalabo - 1 month ago 24
R Question

R: Separate String Without Delimiter (like Fixed Width in Excel)

The dataset looks pretty much like this

Dataset

I searched around but found only the function that needs a delimiter. I managed to import the file to R successfully with two columns.

Then I want to separate DATE column into "Year", "Month", and "Date." So I want to have 4 column in total. And this is where I got stock. The column doesn't have usual "/" or "-" that usually come with the date format.

Thanks for your help.

Answer

As @alistaire has shown, you can convert what you have to an R recognised date format with (replace the single character string in the below with your column vector df$DATE to work on the entire data frame):

date <- as.Date( '19981201', '%Y%m%d' )
date
[1] "1998-12-01"

From there, you can separate out your year, month, and day as you please.

year <- format( date, "%Y")
year
[1] "1998"

month <- format( date, "%m" )
month
[1] "12"

day <- format( date, "%d" )
day
[1] "01"

Of course, you could also skip the date step, and just split the first 8 characters into 3 shorter strings (as @warmoverflow has suggested), but I'd recommend the above as probably better in practice. Mostly because you'll be best using the date format for things like sorting and plotting, so it would be a good idea to use it along the way too.