解析R列表中的值

问题描述 投票:0回答:1

我正在使用openxlsx从多个Excel文件表中读取数据。以下是代码:

tfile <- "/home/mycomp/Documents/latest.xlsx"
sheets <- openxlsx::getSheetNames(tfile)
x <- lapply(sheets, function(X) openxlsx::read.xlsx(tfile, sheet = X,startRow=1, rows=1,cols=1,sep.names=" "))

本质上,我试图仅读取表的第一行。第一行是合并的单元格。 x的输出看起来像这样:

> x
[[1]]
NULL

[[2]]
[1] Total Secondages
<0 rows> (or 0-length row.names)

[[3]]
[1] Product Groups (duration in secondages)
<0 rows> (or 0-length row.names)

我只想提取第一个元素中的值,例如Total SecondagesProduct Groups。我尝试使用unlist,获取列表的名称等,但它们都返回logical(0)

我想将这些值提取为字符向量,例如('Total Secondages', 'Product Groups')等。>

我正在使用openxlsx从多个Excel文件表中读取数据。以下是代码:tfile

r list
1个回答
0
投票

问题是您有空的data.frame,可以从中提取名称。

© www.soinside.com 2019 - 2024. All rights reserved.