我有如下所示的数据,由于大小限制,图像:

因此,我的目标是以bottleneck_list为例,它是一个数据格式列表,并使用lapply,对所有数据执行相同的分析。它们是自定义的,但我很难让它在每个df中运行。
test是bottleneck_list[[1]]
下面是函数的示例
test2 <- test %>%
mutate(early_startTime = startTime - 300) %>%
mutate(id = rownames(test))
loop <- lapply(1:nrow(test), function(x) {
neck_row <- test[x,]
test_list <- test[which(test$startTime == bottleneck_row$endTime),]
})
match <- do.call(rbind,loop)
events * match因此,基本上每个dataframe都必须对其执行几个操作。
以下是我尝试过的:
list_alt <- lapply(bottleneck_list, sapply, function(x) {
test <- bottleneck_list[[x]]
test2 <- test %>%
mutate(early_startTime = startTime - 300) %>%
mutate(id = rownames(test))
loop <- lapply(1:nrow(test), function(x) {
neck_row <- test[x,]
test_list <- test[which(test$startTime == bottleneck_row$endTime),]
})
match <- do.call(rbind,loop)
match
})但不起作用。最终的结果应该是我从这个列表中得到的所有数据,也就是那些带有两个变量的数据的列表。
编辑:我需要引用bottleneck_list[[1]]才能工作。

发布于 2020-06-25 19:52:43
如果是list of data.frames,则在list上循环map,并在data.frame的每个data.frame中创建新列。
library(purrr)
library(dplyr)
bottleneck_list2 <- map(bottleneck_list, ~ .x %>%
mutate(early_startTime = startTime - 300, id = row_number()))或者使用来自base R的base R
bottleneck_list2 <- lapply(bottleneck_list, function(test)
transform(test, early_startTime = startTime - 300, id = row.names(test)))如果我们想要做一些转换
lapply(bottleneck_list, function(test){
row.names(test) <- NULL
test$somecol <- test$col1 + 24
test
}) https://stackoverflow.com/questions/62583279
复制相似问题