使用来自另一个数据帧的条件创建数据子集



我想使用dplyr之类的东西来使用来自另一个数据帧的条件从一个数据帧创建数据子集。所以在一个数据框中,我有一组最小和最大年份的数据以及其他海平面数据lsp,在另一个框架中,我有一个海洋动力学的时间序列。对于lsp数据框中的每一行,我想提取dynamics数据框中最小和最大年龄之间的每一年,并创建一个数据子集。我认为这需要一个for循环。有人知道这是可能的吗?

以LSP第1行为例的期望输出:

第1行LSP(简化)为:

tbody> <<tr>
年龄最小值 年龄最大值
19972007

这是Mapsubset的基本R选项-

Map(function(x, y) subset(dynamics, Year >= x & Year <= y), 
LSP$Age.min, LSP$Age.max)

同样的逻辑也可以用tidyverse函数来实现。

library(dplyr)
library(purrr)
map2(LSP$Age.min, LSP$Age.max, ~dynamics %>% filter(Year >= .x & Year <= .y))

只要你的数据集不是很大,我就会采用下面的方法。

  1. 将(嵌套的)动态数据集添加到lsp数据集的每一行
  2. 打开动态数据集以获得每年一行
  3. 过滤掉不相关的年份

(可选)
  1. 将动态列重新设置为每个lsp记录一行,其中包含动态集中所有相关年份的标签。
lsp %>%
add_column(dynamics %>% nest(data = everything())) %>%
unnest(data) %>%
filter(year >= min & year <= max) %>%
nest(filtered = c(year, value))

我猜这是你想做的。首先给你的输入数据赋名字,这样以后你就知道我的代码是什么意思了。

lsp <- structure(list(Depth = c(0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 
8.5, 10.5, 13.5, 14.5, 18.5, 19.5, 27.5, 28.5, 32, 35.5, 40.5, 
41.5), RSL = c(0.03, 0.03, 0.01, 0.01, -0.04, -0.01, -0.03, 0, 
0.04, 0.03, 0, -0.01, -0.05, -0.07, -0.19, -0.24, -0.31, -0.31, 
-0.27, -0.29), RSL_err_1sig = c(0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 
0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 
0.1), Age_mean = c(2001.754499, 1994.278776, 1987.678949, 1980.805889, 
1973.270485, 1965.018421, 1957.442729, 1952.134369, 1949.031929, 
1945.148184, 1939.132213, 1936.957531, 1927.311071, 1924.379033, 
1897.26123, 1892.977317, 1876.1995, 1858.135589, 1825.967544, 
1820.605298), Age.min = c(1996.752238, 1985.111654, 1977.483594, 
1968.26211, 1961.886124, 1958.219318, 1947.496532, 1943.084044, 
1941.761439, 1935.843414, 1923.952516, 1920.057048, 1906.228232, 
1902.242998, 1875.327613, 1869.925103, 1834.992176, 1811.928966, 
1784.998245, 1767.524866), Age.max = c(2006.75676, 2003.445898, 
1997.874304, 1993.349668, 1984.654846, 1971.817524, 1967.388926, 
1961.184694, 1956.302419, 1954.452954, 1954.31191, 1953.858014, 
1948.39391, 1946.515068, 1919.194847, 1916.029531, 1917.406824, 
1904.342212, 1866.936843, 1873.68573)), class = "data.frame", row.names = c(NA, 
-20L))
dynamics <- structure(list(Year = 1815:1820, dynamics = c(-76.01893261, -64.50519732, 
-66.06270761, -76.22822397, -72.35960029, -77.34157443)), row.names = c(NA, 
6L), class = "data.frame")

然后得到子集的实际代码。

# first get info of years from the "lsp" dataset
# following your example in your comments
year_min  <- list()
year_max  <- list()
all_years <- list()
for(i in 1:nrow(lsp)){
year_min[[i]] <- round(lsp$Age.min[[i]])
year_max[[i]] <- round(lsp$Age.max[[i]])
all_years[[i]] <- c(year_min[[i]]:year_max[[i]])
all_years[[i]] <- as.data.frame(all_years[[i]])
colnames(all_years[[i]]) <- "Year"
}
# now join the info on "Year" from "lsp" data with "dynamics" data to get the subset
library(dplyr)
subset_output <- list()
for (i in 1:length(all_years)){
subset_output[[i]] <- left_join(dynamics,all_years[[i]])
}