【发布时间】:2020-10-16 15:52:30
【问题描述】:
LApply-ception? 无论如何。
我有一个数据框列表。我通过它并进行调整就好了。但是我需要应用每个单独的数据帧,即遍历列表中的每个数据帧。
所以下面的作品。
#THIS WORKS
bottleneck_fury <- function(bottleneck2) {
bottleneck2 <- bottleneck2 %>%
mutate(startTime = as_datetime(startTime, tz = "")) %>%
mutate(endTime = as_datetime(endTime, tz = "")) %>%
mutate(early_startTime = startTime - 300) %>% #5 min prior
mutate_at(c("startTime", "endTime", "early_startTime"),anytime) %>% #formatted time
mutate(id = rownames(bottleneck2))
bottleneck2
}
all_necks <- lapply(bottleneck_test, bottleneck_fury)
dd <- all_necks[[1]]
OUTPUT
dd <- structure(list(startTime = structure(c(1533122400, 1533132060,
1533151920, 1533205740, 1533207960), class = c("POSIXct", "POSIXt"
), tzone = ""), endTime = structure(c(1533131340, 1533132540,
1533153300, 1533207660, 1533218460), class = c("POSIXct", "POSIXt"
), tzone = ""), impact = c(627.06, 26.53, 24.34, 166.84, 761.39
), impactPercent = c(1444.6, 33.98, 30.98, 320.75, 1632.12),
impactSpeedDiff = c(25814.55, 806.43, 733.5, 6289.26, 30350.4
), maxQueueLength = c(4.829494, 3.605648, 2.241074, 5.760513,
5.760513), tmcs = list(c("110N04623", "110-04623", "110N04624",
"110-04624", "110N04625", "110-04625", "110N04626", "110-04626"
), c("110N04623", "110-04623", "110N04624", "110-04624",
"110N04625", "110-04625"), c("110N04623", "110-04623", "110N04624",
"110-04624"), c("110N04623", "110-04623", "110N04624", "110-04624",
"110N04625", "110-04625", "110N04626", "110-04626", "110N04627"
), c("110N04623", "110-04623", "110N04624", "110-04624",
"110N04625", "110-04625", "110N04626", "110-04626", "110N04627"
)), early_startTime = structure(c(1533122100, 1533131760,
1533151620, 1533205440, 1533207660), class = c("POSIXct",
"POSIXt"), tzone = ""), id = c("1", "2", "3", "4", "5")), row.names = c(NA,
5L), class = "data.frame")
现在我拉出这个数据框并对其进行操作,我想在它上面运行一个循环。但是,当我需要它作为数据框时,结果是一个列表。此处的第二个 lapply 现在已更改为代表实际情况以及 unlist() 不起作用的原因。
#DOESNT WORK
bottleneck_fury <- function(bottleneck2) {
# bottleneck2 <- data.frame(matrix(unlist(bottleneck), nrow=length(bottleneck), byrow=T))
bottleneck2 <- bottleneck2 %>%
mutate(startTime = as_datetime(startTime, tz = "")) %>%
mutate(endTime = as_datetime(endTime, tz = "")) %>%
mutate(early_startTime = startTime - 300) %>% #5 min prior
mutate_at(c("startTime", "endTime", "early_startTime"),anytime) %>% #formatted time
mutate(id = rownames(bottleneck2))
bottleneck2
#md_events2 <- md_events
test <- lapply(1:nrow(bottleneck2), function(x) {
#go row by row
bottleneck_row <- bottleneck2[x,]
events <- iris[which(bottleneck_row$maxQueueLength < iris$Petal.Length ==TRUE),]
ID <- do.call("rbind",replicate(nrow(events), bottleneck_row, simplify = FALSE))
cbind(events, ID)
}
}
all_necks <- lapply(bottleneck_test, bottleneck_fury)
dd <- all_necks[[1]]
View(dd)
我希望 R 可以在 lapply 中执行 lapply 并为列表中的两个数据框提供一个新的数据框。
最后,dd 应该是一个数据帧,它是列表all_necks 中的第一个数据帧。
【问题讨论】:
-
1) 你有两个同名的函数,问题出在第二个吗? 2)运行第一个函数后数据集是
dd? -
问题是 #2,是的,这是工作函数之后的 dd
-
你能分享你的原始数据
bottleneck_test并显示预期的输出吗? -
即使一个子集太大,但很高兴在聊天室中通过私人消息这样做