简体   繁体   English

R将列表列表转换为dataframe

[英]R convert list of lists to dataframe

I need to process data that is being provided in a password protected Excel (xlsx) workbook. 我需要处理在受密码保护的Excel(xlsx)工作簿中提供的数据。 For legal reasons, I cannot create an unprotected Excel file or a csv file etc and process from there. 出于法律原因,我无法从那里创建未受保护的Excel文件或csv文件等。 None of the Excel import packages can deal with password protected workbooks. 所有Excel导入包都不能处理受密码保护的工作簿。

From this answer Import password-protected xlsx workbook into R I have managed to extract the data. 从这个答案导入密码保护的xlsx工作簿到R我已设法提取数据。 However it is imported in a list of character lists format. 但是,它是以字符列表格式列表导入的。 The dput of my list looks like this: 我的列表的输入看起来像这样:

list(list("ID", "ID1", "ID2"),
     list("V2", NULL, "text2"),
     list("Name", "John Smith", "Mary Brown"),
     list("Score", 1, 2),
     list("email", "JS@gmail.com", "MB@gov.uk"))

What I want is a dataframe with columns ID, V2 etc that looks like this: 我想要的是具有列ID,V2等的数据帧,如下所示:

   ID    V2     Name        Score  email
   ID1   NULL   John Smith  1      JS@gmail.com
   ID2   text2  Mary Brown  2      MS@gov.uk

There are empty cells in the original Excel workbook, so solutions with unlist will not work. 原始Excel工作簿中有空单元格,因此使用unlist的解决方案将不起作用。

Using a combination of answers from R list to data frame and other similar questions, I have the following code (where listform is the name of the list): 使用从R列表到数据框和其他类似问题的答案组合,我有以下代码(其中listform是列表的名称):

matform <- as.matrix(sapply(listform, function(s) s)) # retains empty
df <- data.frame(matform[2:nrow(matform),])
names(df) = matform[1,]

This is close, but the dataframe has lists as columns. 这很接近,但数据框列出了列。 So str(df) yields: 所以str(df)产量:

'data.frame':   2 obs. of  5 variables:
 $ ID:List of 2
  ..$ : chr "ID1"
  ..$ : chr "ID2"
 $ V2:List of 2
  ..$ : NULL
  ..$ : chr "text2"
and so on

First step: extract the names: 第一步:提取名称:

names = lapply(listform, `[[`, 1)
data = setNames(lapply(listform, `[`, -1), names)

Second step: unlist data and bind columns 第二步:取消列出数据和绑定列

result = as.data.frame(sapply(data, unlist))

The magic here happens in sapply , which internally calls simplify2array . 这里的魔力发生在sapply ,内部调用simplify2array as.data.frame at the end is needed to make a data.frame out of the resulting matrix, and to assign proper data types to the individual columns. 最后需要as.data.frame来从结果矩阵中生成data.frame,并为各列分配适当的数据类型。

The above code has got one problem though: the column types probably aren't what you want. 上面的代码有一个问题:列类型可能不是你想要的。 This can be fixed as follows: 这可以修复如下:

col_classes = sapply(lapply(listform, `[[`, 2), typeof)
result = as.data.frame(sapply(data, unlist), stringsAsFactors = FALSE)
for (col in seq_len(ncol(result)))
    class(result[[col]]) = col_classes[col]

Now you'll get the following result: 现在,您将获得以下结果:

> str(result)
'data.frame':   2 obs. of  5 variables:
 $ ID   : chr  "ID1" "ID2"
 $ V2   : chr  "text1" "text2"
 $ Name : chr  "John Smith" "Mary Brown"
 $ Score: num  1 2
 $ email: chr  "JS@gmail.com" "MB@gov.uk"

And this, I think, is what you want. 我认为,这就是你想要的。

"SetDT" from the "data.table" package seems to be very powerful: “data.table”包中的“SetDT”似乎非常强大:

> library(data.table)

> null2na <- function(x){ ifelse(is.null(x),NA,x)}

> f <- function(x){sapply(x,null2na)}

> L <- list(list("ID", "ID1", "ID2"),
+           list("V2", NULL, "text2"),
+           list("Name", "John Smith", "Mary Brown"),
+           list("S ..." ... [TRUNCATED] 

> L <- setDT(L)[, lapply(.SD, f)]

> setnames(L,colnames(L),unlist(L[1,]))

> L <- L[-1,]

> L
    ID    V2       Name Score        email
1: ID1    NA John Smith     1 JS@gmail.com
2: ID2 text2 Mary Brown     2    MB@gov.uk

> str(L)
Classes ‘data.table’ and 'data.frame':  2 obs. of  5 variables:
 $ ID   : chr  "ID1" "ID2"
 $ V2   : chr  NA "text2"
 $ Name : chr  "John Smith" "Mary Brown"
 $ Score: chr  "1" "2"
 $ email: chr  "JS@gmail.com" "MB@gov.uk"
 - attr(*, ".internal.selfref")=<externalptr> 
> 

(A data table is a better data frame.) (数据表是更好的数据框。)

The function "f" does two jobs: It "unlist"s and turns NULL into NA. 函数“f”执行两个作业:它“取消列出”并将NULL转换为NA。

Here's one way using data.table v1.9.5 (for the transpose() function): 这是使用data.table v1.9.5 (对于transpose()函数)的一种方式:

require(data.table) # v1.9.5+
setDT(sapply(ll, function(x) setattr(transpose(x[-1L]), 'names', x[[1L]])))[]
#     ID    V2       Name Score        email
# 1: ID1    NA John Smith     1 JS@gmail.com
# 2: ID2 text2 Mary Brown     2    MB@gov.uk

Use setDF() instead of setDT() to return a data.frame instead. 使用setDF()而不是setDT()来返回data.frame

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM