我有以下tibble和数据框的嵌套列表:
>source
# A tibble: 6 × 2
lon lat
<dbl> <dbl>
1 6.02 55.1
2 6.02 55.0
3 6.02 54.9
>dest
[[1]][[1]]
lon lat
1 54.98908 6.900084
2 54.92777 6.772623
3 55.09501 6.911837
[[1]][[2]]
lon lat
1 54.98908 6.900084
2 54.92777 6.772623
3 55.09501 6.911837
[[1]][[3]]
lon lat
1 54.98908 6.900084
2 54.92777 6.772623
3 55.09501 6.911837
[[2]][[1]]
lon lat
1 54.98908 6.900084
2 54.92777 6.772623
3 55.09501 6.911837
[[2]][[2]]
lon lat
1 54.98908 6.900084
2 54.92777 6.772623
3 55.09501 6.911837
[[2]][[3]]
lon lat
1 54.98908 6.900084
2 54.92777 6.772623
3 55.09501 6.911837
我想对tible source中的行和dest中的每个“块”应用一个函数。
示例:
源代码中的row 1
应应用于dest[[1]][[1]]
和dest[[2]][[1]]
中的每行
源代码中的row 2
应应用于dest[[1]][[2]]
和dest[[2]][[2]]
中的每行
源代码中的row 3
应应用于dest[[1]][[3]]
和dest[[2]][[3]]
中的每行
和/或其他信息。
我怎么能做到这一点呢?我被apply,lappl和mapy弄得一团糟,我会很感激任何帮助。
source<-structure(list(lon = c(6.02125801226333, 6.02125801226333, 6.02125801226333,
6.02125801226333, 6.02125801226333, 6.02125801226333), lat = c(55.0579432585625,
54.9681151832365, 54.8782857724705, 54.7884550247254, 54.6986229384757,
54.6087895122085)), row.names = c(NA, -6L), class = c("tbl_df",
"tbl", "data.frame"))
dest<-list(list(structure(list(lon = c(55.0446726604773, 55.0911992769466,
55.1399831259253), lat = c(6.11070373013145, 5.93718385855719,
6.05909963519238)), class = "data.frame", row.names = c(NA, -3L
)), structure(list(lon = c(54.963042116042, 54.9238652445021,
54.9948148730435), lat = c(6.11154210955708, 6.10009257140253,
5.93487232950475)), class = "data.frame", row.names = c(NA, -3L
)), structure(list(lon = c(54.9181540526, 54.9628448755405, 54.8174082489187
), lat = c(5.94011737583315, 5.98947008604159, 6.08806491235748
)), class = "data.frame", row.names = c(NA, -3L)), structure(list(
lon = c(54.7263291045393, 54.8728552727446, 54.8675223815364
), lat = c(5.95561986508533, 6.0534792303467, 5.97754320721106
)), class = "data.frame", row.names = c(NA, -3L)), structure(list(
lon = c(54.7185472365059, 54.7069293987346, 54.78280968399
), lat = c(5.93305860952388, 5.93121414118021, 5.9884946645099
)), class = "data.frame", row.names = c(NA, -3L)), structure(list(
lon = c(54.560413160877, 54.5853088068835, 54.5185005363673
), lat = c(6.0976246910947, 5.93394019791707, 6.02387338808233
)), class = "data.frame", row.names = c(NA, -3L))), list(
structure(list(lon = c(55.050226235055, 55.0240838617402,
54.9636263846607), lat = c(5.90235917535441, 5.90965086672992,
5.97880750058409)), class = "data.frame", row.names = c(NA,
-3L)), structure(list(lon = c(55.0746706563331, 55.0478637437921,
54.8541974469044), lat = c(5.98859383669152, 5.92618888252071,
6.04742105597978)), class = "data.frame", row.names = c(NA,
-3L)), structure(list(lon = c(54.7575000883344, 54.7676512681177,
54.9427732774055), lat = c(6.06061526193956, 6.09764527834345,
5.90903632630959)), class = "data.frame", row.names = c(NA,
-3L)), structure(list(lon = c(54.7776555082601, 54.8462348683655,
54.7620026570004), lat = c(6.1346781687426, 6.12031707754559,
5.91627897917598)), class = "data.frame", row.names = c(NA,
-3L)), structure(list(lon = c(54.6176186034159, 54.7833923796146,
54.6922873458308), lat = c(6.10088997672983, 6.09177636538747,
6.14915348430183)), class = "data.frame", row.names = c(NA,
-3L)), structure(list(lon = c(54.5680535136696, 54.5386600427152,
54.5879440622283), lat = c(6.13919150641202, 5.91144136237118,
5.89113937054887)), class = "data.frame", row.names = c(NA,
-3L))))
3条答案
按热度按时间dfddblmv1#
我们可以将源代码按行
split
到列表中,然后将mapply
与lapply
一起使用:使用
dplyr::bind_cols
作为要应用的函数的示例。输出:
cfh9epnr2#
一个循环就可以做到这一点(这里的函数是一个简单的加法):
输出量:
41zrol4v3#
如果我跟着,每个目的地都在赤道附近,每个源都在北方,对于每个目的地,你要把源的纬度和经度相加,这样你就可以计算两者之间的距离。
因此,结果应类似于:
这段代码将完成这一点。如果你正在处理一个大的数据集,这段代码可能会更有效。