从数据结构中间层中剔除值
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了从数据结构中间层中剔除值相关的知识,希望对你有一定的参考价值。
- 我有一个列表,其中每个项目是一个引用excel工作簿的列表。
- 在这些工作簿中,每个项目都是一个列表,指的是该工作簿中的工作表。
- 在这些工作表中,每个项目都是一个参考该工作表上的列名称的向量。
- 我只对每个向量中每个工作表的第一列的名称感兴趣
通过这些数字,我创建了一个新的结构,可以插入到数据框中
- 每行仍将引用工作表
- 点中的每个项目都是所有第一列名称的向量
由于要读取的数据类型的性质,粘贴样本数据有点困难,所以希望它从我的描述中足够清楚。
因此,如果l1
代表上述列表,那么l2
代表以下列表:
list(c((Worksheet1, Sheet1, A1), (Worksheet1, Sheet2, A1), (Worksheet1, Sheet3, A1)),
c((Worksheet2, Sheet1, A1), (Worksheet2, Sheet2, A1), (Worksheet2, Sheet3, A1))
,
从而返回左上角单元格的向量列表
我可以使用同心for循环来完成这个,但是我无法弄清楚在这里使用apply系列(或地图系列)的措辞。如何以这种方式跟踪列表?
我最好的尝试是利用我的知识(不只是吐出错误)
l2 <- lapply(l1, "[[", 1)
但是,它返回每个工作簿的第一张表中的所有列的向量,这不是我所追求的。
dput编辑:
structure(list(`Sheet 1` = structure(list(Sheet1 = c("Claim Status Report",
"X__1", "Report A0817B"), Sheet2 = c("Standard Claims Lodgement-year Costs Breakdown Summary",
"X__1", "X__2", "X__3", "X__4", "X__5", "X__6", "X__7", "X__8",
"X__9", "X__10", "X__11", "X__12", "X__13", "X__14", "X__15",
"X__16", "X__17", "X__18", "X__19"), Sheet3 = c("Standard Claims Lodgement-year Costs Breakdown by Claim",
"X__1", "X__2", "X__3", "X__4", "X__5", "X__6", "X__7", "X__8",
"X__9", "X__10", "X__11", "X__12", "X__13", "X__14", "X__15",
"X__16", "X__17", "X__18", "X__19", "X__20", "X__21"), Sheet4 = "Claims with Statistical Case Estimate Variations over $1000",
Sheet5 = "Claims Received from 01/01/2018 to 31/01/2018",
Sheet6 = "Minor Claims received between 01/01/2018 and 31/01/2018",
Sheet7 = c("Medical Certificates Received between 01/01/2018 and 31/01/2018",
"X__1", "X__2", "X__3", "X__4", "X__5", "X__6", "X__7", "X__8",
"X__9", "X__10", "X__11"), Sheet8 = c("All (net) Payments entered between 01/01/2018 and 31/01/2018",
"X__1", "X__2", "X__3", "X__4", "X__5", "X__6", "X__7", "X__8",
"X__9", "X__10")), .Names = c("Sheet1", "Sheet2", "Sheet3",
"Sheet4", "Sheet5", "Sheet6", "Sheet7", "Sheet8")), `Sheet 2` = structure(list(
Sheet1 = c("Claim Status Report", "X__1", "Report A0817B"
), Sheet2 = c("Standard Claims Lodgement-year Costs Breakdown Summary",
"X__1", "X__2", "X__3", "X__4", "X__5", "X__6", "X__7", "X__8",
"X__9", "X__10", "X__11", "X__12", "X__13", "X__14", "X__15",
"X__16", "X__17", "X__18", "X__19"), Sheet3 = c("Standard Claims Lodgement-year Costs Breakdown by Claim",
"X__1", "X__2", "X__3", "X__4", "X__5", "X__6", "X__7", "X__8",
"X__9", "X__10", "X__11", "X__12", "X__13", "X__14", "X__15",
"X__16", "X__17", "X__18", "X__19", "X__20", "X__21"), Sheet4 = "Claims with Statistical Case Estimate Variations over $1000",
Sheet5 = c("Medical Certificates Received between 01/12/2017 and 31/12/2017",
"X__1", "X__2", "X__3", "X__4", "X__5", "X__6", "X__7", "X__8",
"X__9", "X__10", "X__11"), Sheet6 = c("All (net) Payments entered between 01/12/2017 and 31/12/2017",
"X__1", "X__2", "X__3", "X__4", "X__5", "X__6", "X__7", "X__8",
"X__9", "X__10")), .Names = c("Sheet1", "Sheet2", "Sheet3",
"Sheet4", "Sheet5", "Sheet6"))), .Names = c("Sheet 1", "Sheet 2"
))
答案
试试这个:
l2<-lapply(l1,function(x){tmp<-sapply(x,"[[",1,USE.NAMES = FALSE);mapply(c,names(tmp),tmp,SIMPLIFY = FALSE,USE.NAMES = FALSE)})
desired<-mapply(function(x,y){lapply(y,function(z){c(x,z)})},names(l2),l2,USE.NAMES = FALSE)
以上是关于从数据结构中间层中剔除值的主要内容,如果未能解决你的问题,请参考以下文章
如何将数据从顶点着色器传递到片段着色器,中间有着色器[重复]