我有一个数据框,其中日期和时间混合如下:
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
我想分割日期和时间,以便DD,MM,YY,HH和MM分成如下单独的列:
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
有什么想法吗?感谢
答案 0 :(得分:3)
一种选择是使用extract
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
或者您可以使用strsplit
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
答案 1 :(得分:3)
为了好玩,以下是另外三种选择:
format
within(df, {
var <- paste(DDMMYY, HHMM)
var <- strptime(var, format = "%d%m%y %H%M")
mm <- format(var, "%M")
hh <- format(var, "%H")
YY <- format(var, "%y")
MM <- format(var, "%m")
DD <- format(var, "%d")
rm(var, DDMMYY, HHMM)
})
# ID DD MM YY hh mm
# 1 1 10 03 10 22 05
# 2 2 11 03 10 10 45
# 3 3 12 03 10 11 10
# 4 4 13 03 10 22 50
read.fwf
(和一堆其他嵌套的东西)cbind(df[1],
setNames(do.call(
cbind, lapply(df[-1], function(x) {
read.fwf(textConnection(as.character(x)),
widths = rep(2, nchar(x[1])/2))
})),
c("DD", "MM", "YY", "hh", "mm")))
# 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
separate
library(dplyr)
library(tidyr)
df %>%
separate(DDMMYY, into = c("DD", "MM", "YY"), sep = c(2, 4)) %>%
separate(HHMM, into = c("hh", "mm"), sep = 2)
# ID DD MM YY hh mm
# 1 1 10 03 10 22 05
# 2 2 11 03 10 10 45
# 3 3 12 03 10 11 10
# 4 4 13 03 10 22 50