Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
851 views
in Technique[技术] by (71.8m points)

dataframe - R split column by fixed width

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...