0

语境

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

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

在此处输入图像描述

        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 PMinstrsplitMike.Snow2013-02-25 14:44:00名称类似于Userand Time
  2. 日期部分属于 POSIXlt 类

换句话说,结果应该是这样的......

> 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" 
4

1 回答 1

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"
于 2013-04-04T03:59:16.043 回答