Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R split column by fixed width

Tags:

dataframe

r

I have a dataframe where the date and time are mixed like this:

ID <- c(1,2,3,4)
DDMMYY <-c(100310,110310,120310,130310)
HHMM <- c(2205,1045,1110,2250)
df <- data.frame(ID,DDMMYY,HHMM)
df

ID  DDMMYY  HHMM
1   100310  2205
2   110310  1045
3   120310  1110
4   130310  2250

I want to split the date and time so that DD, MM, YY, HH and MM fall into separate columns like this:

ID  DD  MM  YY  HH  MM
1   10  3   10  22  5
2   11  3   10  10  45
3   12  3   10  11  10
4   13  3   10  22  50

Any idea? Thanks

like image 438
Filly Avatar asked Mar 29 '26 21:03

Filly


1 Answers

One option would be to use extract from tidyr

library(tidyr)
extract(extract(df, DDMMYY, c("DD","MM", "YY"), "(..)(..)(..)",
          convert=TRUE), HHMM, c("HH", "MM"), "(..)(..)", convert=TRUE)
#  ID DD MM YY HH MM
#1  1 10  3 10 22  5
#2  2 11  3 10 10 45
#3  3 12  3 10 11 10
#4  4 13  3 10 22 50

Or you could use strsplit from base R

 df[,c("DD", "MM", "YY", "HH", "MM")] <- do.call(data.frame,lapply(df[,-1],
       function(x) do.call(rbind,lapply(strsplit(as.character(x),
                     '(?<=..)(?=..)', perl=TRUE), as.numeric))))

 df[,-(2:3)]
 #  ID DD MM YY HH MM.1
 #1  1 10  3 10 22    5
 #2  2 11  3 10 10   45
 #3  3 12  3 10 11   10
 #4  4 13  3 10 22   50
like image 144
akrun Avatar answered Apr 02 '26 08:04

akrun



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!