我如何连接元素(列从dataframe)从两个列表通过行名使用R?



我需要帮助。我有两个列表:第一个包含不同点的ndvi时间序列,第二个包含相同图的降水时间序列(图在两个列表中的顺序相同)。

我需要合并这两个列表。我想将一个列表中的降水列添加到另一个列表中对应的ndvi列中,这是关于日期的(在这里用行名中的字母表示),以便对列之间的相关性进行后检验分析。而ndvi和降水时间序列都具有不同的长度和不同的日期。

我创建了两个列表作为我的数据集的示例。但是,在我的实际数据集中,行名是每月的日期,格式为&;%Y-%m-%d&;

library(tidyverse)
set.seed(100)
# First variable is ndvi.mon1 (monthly ndvi)
ndvi.mon1 <- vector("list", length = 3)
for (i in seq_along(ndvi.mon1)) {
aux <- data.frame(ndvi = sample(randu$x,
sample(c(seq(1,20, 1)),1),
replace = T))

ndvi.mon1[i] <- aux
ndvi.mon1 <- ndvi.mon1 %>% map(data.frame)
rownames(ndvi.mon1[[i]]) <- sample(letters, size=seq(letters[1:as.numeric(aux %>% map(length))]) %>% length)
}
# Second variable is precipitation
precipitation <- vector("list", length = 3)
for (i in seq_along(ndvi.mon1)){
prec_aux <- data.frame(precipitation = sample(randu$x*500,
26,
replace = T))
row.names(prec_aux) <-  seq(letters[1:as.numeric(prec_aux %>% map(length))])

precipitation[i] <- prec_aux
precipitation <- precipitation %>% map(data.frame)
rownames(precipitation[[i]]) <- letters[1:(as.numeric(precipitation[i] %>% map(dim) %>% map(first)))]  
}

有人能帮帮我吗?

谢谢! !

Marcio .

也许像这样?

library(dplyr)
library(purrr)
precipitation2 <- precipitation %>% 
map(rownames_to_column) %>% 
map(rename, precipitation = 2)
ndvi.mon2 <- ndvi.mon1 %>% 
map(rownames_to_column) %>% 
map(rename, ndvi = 2)

purrr::map2(ndvi.mon2, precipitation2, left_join, by = "rowname")
[[1]]
rowname     ndvi precipitation
1        k 0.354886      209.7415
2        x 0.596309      103.3700
3        r 0.978769      403.8775
4        l 0.322291      354.2630
5        c 0.831722      348.9390
6        s 0.973205      273.6030
7        h 0.949827      218.6430
8        y 0.443353       61.9310
9        b 0.826368        8.3290
10       d 0.337308      291.2110

下面将返回已合并的data.frames列表,使用rownames:

lapply(seq_along(ndvi.mon1), function(i) {
merge(
x = data.frame(date = rownames(ndvi.mon1[[i]]), ndvi = ndvi.mon1[[i]][,1]),
y = data.frame(date = rownames(precipitation[[i]]), precip = precipitation[[i]][,1]),
by="date"
)
})

输出:

[[1]]
date     ndvi   precip
1     b 0.826368   8.3290
2     c 0.831722 348.9390
3     d 0.337308 291.2110
4     h 0.949827 218.6430
5     k 0.354886 209.7415
6     l 0.322291 354.2630
7     r 0.978769 403.8775
8     s 0.973205 273.6030
9     x 0.596309 103.3700
10    y 0.443353  61.9310
[[2]]
date     ndvi   precip
1    g 0.415824 283.9335
2    k 0.573737 311.8785
3    p 0.582422 354.2630
4    y 0.952495 495.4340
[[3]]
date     ndvi   precip
1     b 0.656463 332.5700
2     c 0.347482  94.7870
3     d 0.215425 431.3770
4     e 0.063100 499.2245
5     f 0.419460 304.5190
6     g 0.712057 226.7125
7     h 0.666700 284.9645
8     i 0.778547 182.0295
9     k 0.902520  82.5515
10    l 0.593219 430.6630
11    m 0.788715 443.5345
12    n 0.347482 132.3950
13    q 0.719538  79.1835
14    r 0.911370 100.7025
15    s 0.258743 309.3575
16    t 0.940644 142.3725
17    u 0.626980 335.4360
18    v 0.167640 390.4915
19    w 0.826368  63.3760
20    x 0.937211 439.8685

最新更新