1

我正在将循环for()操作转换为apply()家庭操作:(sapply、、、lapplymapply)以在我的所有 R 代码中获得性能。我有一个列表,名称lt如下:

lt

$`11141550000`
            y
2016-02-05 18
2016-03-03  8
2016-03-30  6
2016-04-26  0

$`11140780000`
           y
2016-03-25 2
2016-03-30 0
2016-04-04 0
2016-04-09 0
2016-04-14 0

$`11141550000`
            y
2016-02-05 18
2016-03-03  8
2016-07-16 10
2016-08-12 10

我的一部分代码非常慢(我知道forR 中的操作效率不高,如果你想在这门语言中变得更专业,应该避免使用)。我在这样的块之前编码:

for (i in 1:length(lt)){
  lt[[i]] <- lt[[i]][as.Date(index(lt[[i]]), format = "%Y-%m-%d") < "2018-11-01"]
}

我正在尝试将其转换for为快速Filteror操作sapplylapply其中每个列表的每个值都必须在 date 之前"2018-11-01"。尽管如此,我还没有能力:

第一次尝试:

f <- function(i){
  lt[[i]][as.Date(index(lt[[i]]), format = "%Y-%m-%d") < "2018-11-01"]
}
lapply(lt, function(x) f(x))

但收到错误:

 Error in lt[[i]] : recursive indexing failed at level 2 

第二次尝试:

f <- function(i){
  lt[[i]][as.Date(index(lt[[i]]), format = "%Y-%m-%d") < "2018-11-01"]
}
Filter(function(x) f, lt)

但我收到了这条消息:

Error in Filter(function(x) f, lt) : 
  (list) object cannot be coerced to type 'logical'

第三次尝试:

Filter(f, lt)

但同样,我收到了一个错误:

Error in lt[[i]] : recursive indexing failed at level 2

我将不胜感激翻译此for操作的任何帮助,因为我需要更好地了解apply函数的动态。

PD1。

感谢 J.Gourlay 的反馈,我正在添加我的列表示例dput

> dput(lt)
structure(list(`11140780000` = structure(c(2, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 0, 4, 0, 0, 0, 0, 0, 0, 1
), .Dim = c(132L, 1L), .Dimnames = list(NULL, "y"), index = structure(c(1458864000, 
1459296000, 1459728000, 1460160000, 1460592000, 1461024000, 1461456000, 
1461888000, 1462320000, 1462752000, 1463184000, 1463616000, 1464048000, 
1464480000, 1464912000, 1465344000, 1465776000, 1466208000, 1466640000, 
1467072000, 1467504000, 1467936000, 1468368000, 1468800000, 1469232000, 
1469664000, 1470096000, 1470528000, 1470960000, 1471392000, 1471824000, 
1472256000, 1472688000, 1473120000, 1473552000, 1473984000, 1474416000, 
1474848000, 1475280000, 1475712000, 1476144000, 1476576000, 1477008000, 
1477440000, 1477872000, 1478304000, 1478736000, 1479168000, 1479600000, 
1480032000, 1480464000, 1480896000, 1481328000, 1481760000, 1482192000, 
1482624000, 1483056000, 1483142400, 1483574400, 1484006400, 1484438400, 
1484870400, 1485302400, 1485734400, 1486166400, 1486598400, 1487030400, 
1487462400, 1487894400, 1488326400, 1488758400, 1489190400, 1489622400, 
1490054400, 1490486400, 1490918400, 1491350400, 1491782400, 1492214400, 
1492646400, 1493078400, 1493510400, 1493942400, 1494374400, 1494806400, 
1495238400, 1495670400, 1496102400, 1496534400, 1496966400, 1497398400, 
1497830400, 1498262400, 1498694400, 1499126400, 1499558400, 1499990400, 
1500422400, 1500854400, 1501286400, 1501718400, 1502150400, 1502582400, 
1503014400, 1503446400, 1503878400, 1504310400, 1504742400, 1505174400, 
1505606400, 1506038400, 1506470400, 1506902400, 1507334400, 1507766400, 
1508198400, 1508630400, 1509062400, 1509494400, 1509926400, 1510358400, 
1510790400, 1511222400, 1511654400, 1512086400, 1512518400, 1512950400, 
1513382400, 1513814400, 1514246400, 1514678400, 1514764800), tzone = "UTC", tclass = "Date"), .indexCLASS = "Date", .indexTZ = "UTC", tclass = "Date", tzone = "UTC", class = c("xts", 
"zoo")), `11141550000` = structure(c(18, 8, 6, 0, 4, 8, 10, 10, 
0, 23, 0, 8, 0, 2, 14, 16, 20, 4, 4, 4), .indexCLASS = "Date", .indexTZ = "UTC", tclass = "Date", tzone = "UTC", class = c("xts", 
"zoo"), index = structure(c(1454630400, 1456963200, 1459296000, 
1461628800, 1463961600, 1466294400, 1468627200, 1470960000, 1473292800, 
1475625600, 1477958400, 1480291200, 1482624000, 1483142400, 1483833600, 
1486166400, 1488499200, 1490832000, 1493164800, 1495497600), tzone = "UTC", tclass = "Date"), .Dim = c(20L, 
1L), .Dimnames = list(NULL, "y"))), .Names = c("11140780000", 
"11141550000"))

PD2。

根据 Jozef 的建议,我确认我已经使用库zoo来重现我的代码。即循环indexzoo包的功能。for

4

1 回答 1

1

本质上,您正在传递一个对象,该对象需要在lapply. 考虑将lt换成seq_along(lt). lapply另外,请记住与for循环不同,应用函数返回对象。所以分配lapply给一个对象。此外,当函数接受一个非可选参数时,无需指定function运算符。

f <- function(i){
  lt[[i]][as.Date(index(lt[[i]]), format = "%Y-%m-%d") < "2018-11-01"]
}

new_lt <- lapply(seq_along(lt), f)

如果您的for循环按您说的那样工作,则 new_lt应该与运行循环后的lt完全相同:for

all.equal(lt_after_for_loop, new_lt)
# [1] TRUE

identical(lt_after_for_loop, new_lt)
# [1] TRUE

或者,传递整个对象,而不是使用调整后定义函数的索引:

f <- function(obj){
  obj[as.Date(index(obj), format = "%Y-%m-%d") < "2018-11-01"]
}

new_lt <- lapply(lt, f)

最后,Filter()通过逻辑条件过滤(保留或删除)列表的对象,而不是列表的每个项目中的内容,除非您使用此类内部内容来决定要从列表中删除的顶级项目。但是for并且lapply不排除处理期间的项目(即,在调用之前/之后的相同数量的项目)。

于 2018-12-18T15:32:23.477 回答