将1行1列数据帧的列表转换为列表列表

时间:2013-04-04 03:26:28

标签: r xlconnect

上下文

我正在使用XLConnect从电子表格中获取一堆Excel命名范围。执行此操作时,即使命名范围是单个单元格,XLConnect也会返回data.frames列表。

所以我有一个像这样的数据框列表,每个数据框包含一行和一列,其值为Mike.Snow - 25/02/2013 2:44:00 PM

enter image description here

        data <- structure(list(BW_SPUserNameTime = structure(list(Col1 = "Mike.Snow - 25/02/2013 2:44:00 PM"), .Names = "Col1", row.names = c(NA, 
-1L), class = "data.frame"), UserNameDateAMCars = structure(list(
    Col1 = "John.Doe  -  25/02/2013 10:40:00 AM"), .Names = "Col1", row.names = c(NA, 
-1L), class = "data.frame"), UserNameDateMJCars = structure(list(
    Col1 = "John.Doe  -  25/02/2013 10:39:00 AM"), .Names = "Col1", row.names = c(NA, 
-1L), class = "data.frame"), UserNameDateOtherCD2CTrains = structure(list(
    Col1 = "John.Doe  -  25/02/2013 10:36:00 AM"), .Names = "Col1", row.names = c(NA, 
-1L), class = "data.frame"), UserNameDateSAP = structure(list(
    Col1 = "John.Doe  -  25/02/2013 10:34:00 AM"), .Names = "Col1", row.names = c(NA, 
-1L), class = "data.frame"), UserNameDateTrainsMineToPort = structure(list(
    Col1 = "John.Doe  -  25/02/2013 10:38:00 AM"), .Names = "Col1", row.names = c(NA, 
-1L), class = "data.frame"), UserNameDateWACLATrains = structure(list(
    Col1 = "John.Doe  -  25/02/2013 10:36:00 AM"), .Names = "Col1", row.names = c(NA, 
-1L), class = "data.frame"), UserNameDateYACLATrains = structure(list(
    Col1 = "John.Doe  -  25/02/2013 10:36:00 AM"), .Names = "Col1", row.names = c(NA, 
-1L), class = "data.frame")), .Names = c("BW_SPUserNameTime", 
"UserNameDateAMCars", "UserNameDateMJCars", "UserNameDateOtherCD2CTrains", 
"UserNameDateSAP", "UserNameDateTrainsMineToPort", "UserNameDateWACLATrains", 
"UserNameDateYACLATrains"))

问题

如何将其转换为list of lists? (具有以下属性)

  1. 内部列表是一个命名列表,其中Mike.Snow - 25/02/2013 2:44:00 PMstrsplit Mike.Snow2013-02-25 14:44:00,其名称为UserTime
  2. 日期部分属于POSIXlt类
  3. 换句话说,结果应该是这样的......

    > data.list <- list(BW_SPUserNameTime  = list(
    +                                           User = c("Mike.Snow"), 
    +                                           Time = as.POSIXlt("2013-02-25 14:44:00")),
    +                    UserNameDateAMCars = list(
    +                                           User = c("John.Doe") , 
    +                                           Time = as.POSIXlt("2013-02-25 10:40:00"))
    +                   )
    > data.list
    $BW_SPUserNameTime
    $BW_SPUserNameTime$User
    [1] "Mike.Snow"
    
    $BW_SPUserNameTime$Time
    [1] "2013-02-25 14:44:00"
    
    
    $UserNameDateAMCars
    $UserNameDateAMCars$User
    [1] "John.Doe"
    
    $UserNameDateAMCars$Time
    [1] "2013-02-25 10:40:00"
    
    
    > sapply(data.list[[1]], class)
    $User
    [1] "character"
    
    $Time
    [1] "POSIXlt" "POSIXt" 
    

1 个答案:

答案 0 :(得分:1)

这是一种使用lapply和自编函数进行处理的方法

process <- function(x, format = '%d/%m/%Y %I:%M:%S %p'){
  foo <- as.list(unlist(strsplit(unlist(x), split = '\\s+-\\s+')))
  foo[[2]] <- as.POSIXlt(foo[[2]], format = format)
  names(foo) <- c('user', 'time')
  foo
}

zz <- lapply(data,process)
str(zz)
# List of 8
# $ BW_SPUserNameTime           :List of 2
# ..$ user: chr "Mike.Snow"
# ..$ time: POSIXlt[1:1], format: "2013-02-25 14:44:00"
# $ UserNameDateAMCars          :List of 2
# ..$ user: chr "John.Doe"
# ..$ time: POSIXlt[1:1], format: "2013-02-25 10:40:00"
# $ UserNameDateMJCars          :List of 2
# ..$ user: chr "John.Doe"
# ..$ time: POSIXlt[1:1], format: "2013-02-25 10:39:00"
# $ UserNameDateOtherCD2CTrains :List of 2
# ..$ user: chr "John.Doe"
# ..$ time: POSIXlt[1:1], format: "2013-02-25 10:36:00"
# $ UserNameDateSAP             :List of 2
# ..$ user: chr "John.Doe"
# ..$ time: POSIXlt[1:1], format: "2013-02-25 10:34:00"
# $ UserNameDateTrainsMineToPort:List of 2
# ..$ user: chr "John.Doe"
# ..$ time: POSIXlt[1:1], format: "2013-02-25 10:38:00"
# $ UserNameDateWACLATrains     :List of 2
# ..$ user: chr "John.Doe"
# ..$ time: POSIXlt[1:1], format: "2013-02-25 10:36:00"
# $ UserNameDateYACLATrains     :List of 2
# ..$ user: chr "John.Doe"
# ..$ time: POSIXlt[1:1], format: "2013-02-25 10:36:00"