如何将带有几行代码的字符数组转换为data.frame?

时间:2016-08-05 20:04:03

标签: r lapply sapply

我有以下数组
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.

我现在最好的事情是

window.onload = function(){
  
  var rangeMain = document.getElementById('main'),
      rangeChild1 = document.getElementById('child1'),
      rangeChild2 = document.getElementById('child2');
  rangeMain.onchange = function(){
      rangeChild1.value = this.value;
      rangeChild2.value = this.value;
  }
  
}

有更优雅的东西吗?

5 个答案:

答案 0 :(得分:3)

来自tstrsplit软件包的{p> data.table和来自str_split_fixed的{​​{1}}是分割字符串向量时获取正确形状数据的非常有用的函数;前者提供了分割字符串的stringr,允许您在不使用transpose函数的情况下单独提取日期和时间,后者将字符串拆分为具有指定列的矩阵:

apply

答案 1 :(得分:2)

my_results <- sapply(strsplit(my_list,"|",fixed=T),function(x) strsplit(x,"--",fixed=T))
my_dates <- t(Reduce("rbind",myresults[1,]))
my_times <- t(Reduce("rbind",myresults[2,]))

答案 2 :(得分:2)

strsplit接受一个可以在一次传递中进行拆分的greppish模式。然后可以使用lapply(或sapply)并使用setNames结束。

 setNames( data.frame(lapply( strsplit( my_vec, split="\\-\\-|\\|"),  "[", 1:2) ), paste0("x",1:3) )

      x1     x2     x3
1 Jan-01 Jan-01 Jul-06
2 Dec-31 Jun-30 Dec-31

显然,可以通过在上面的代码中用3:4代替1:2来处理时间。

答案 3 :(得分:1)

使用stringr的另一种选择:

library(stringr)
a <- t(str_split_fixed(my_list, "\\||--", 4))

#     [,1]     [,2]     [,3]    
#[1,] "Jan-01" "Jan-01" "Jul-06"
#[2,] "Dec-31" "Jun-30" "Dec-31"
#[3,] "00:00"  "12:00"  "09:00" 
#[4,] "24:00"  "18:00"  "19:00" 

要获得最终结果data.frame(a[1:2,])data.frame(a[3:4,])

<强>更新

my_list <- "Jan-01--Dec-31|00:00--24:00"
a <- t(str_split_fixed(my_list, "\\||--", 4))

     [,1]    
[1,] "Jan-01"
[2,] "Dec-31"
[3,] "00:00" 
[4,] "24:00"

data.frame(a[1:2,])

  a.1.2...
1   Jan-01
2   Dec-31

data.frame(a[3:4,])

  a.3.4...
1    00:00
2    24:00

答案 4 :(得分:1)

这是base R选项

lst <- strsplit(scan(text=my_list, sep="|", what ="", quiet=TRUE), "--")
do.call(cbind, lst[c(TRUE, FALSE)])
#     [,1]     [,2]     [,3]    
#[1,] "Jan-01" "Jan-01" "Jul-06"
#[2,] "Dec-31" "Jun-30" "Dec-31"

do.call(cbind, lst[c(FALSE, TRUE)])
#     [,1]    [,2]    [,3]   
#[1,] "00:00" "12:00" "09:00"
#[2,] "24:00" "18:00" "19:00"

或单行base R选项

lapply(split(scan(text=my_list, sep="|", what ="", quiet=TRUE), 1:2), 
                      function(x) do.call(cbind, strsplit(x, "--")))
#$`1`
#     [,1]     [,2]     [,3]    
#[1,] "Jan-01" "Jan-01" "Jul-06"
#[2,] "Dec-31" "Jun-30" "Dec-31"

#$`2`
#    [,1]    [,2]    [,3]   
#[1,] "00:00" "12:00" "09:00"
#[2,] "24:00" "18:00" "19:00"