【发布时间】:2016-12-12 07:54:00
【问题描述】:
我有以下数组my_list <- c("Jan-01--Dec-31|00:00--24:00", "Jan-01--Jun-30|12:00--18:00",
"Jul-06--Dec-31|09:00--19:00")
产生的最短代码是什么:
x1 x2 x3
1 Jan-01 Jan-01 Jul-06
2 Dec-31 Jun-30 Dec-31
和
x2 x2 x3
1 00:00 12:00 09:00
2 24:00 18:00 19:00
目前我有(不是很好)代码
df <- as.data.frame(strsplit(my_list, split = "|", fixed = T),
stringsAsFactors = F)
date_list <- strsplit(as.character(df[1, ]), split = "--", fixed = T)
date_df <- as.data.frame(date_list, col.names = c(1:length(date_list)),
stringsAsFactors = F)
time_list <- strsplit(as.character(df[2, ]), split = "--", fixed = T)
time_df <- as.data.frame(time_list, col.names = c(1:length(date_list)),
stringsAsFactors = F)
到目前为止我所拥有的最好的东西是
date_list <- sapply(strsplit(schedule$schedule, split = "|", fixed = T), "[", 1)
date_df <- t(data.frame(x1=sapply(strsplit(df1, split = "--", fixed = T), "[", 1),
x2=sapply(strsplit(df1, split = "--", fixed = T), "[", 2),
stringsAsFactors = F))
# and similarly for time_list and time_df.
还有什么更优雅的吗?
【问题讨论】: