【发布时间】:2021-11-04 21:16:01
【问题描述】:
我有两个数据集:一个是我研究中的物种以及我观察它们的次数,另一个更大的数据集是更广泛的观察数据库。
我想从另一个数据集中的值中对我的短数据集中的一列进行“观察到的最低纬度”(或最高或平均值)的变异,但我不太清楚如何在变异中匹配它们。
set.seed(1)
# my dataset. sightings isn't important for this, just important that the solution doesn't mess up existing columns.
fake_spp_df <- data.frame(
species = c("a","b","c","d",'e'),
sightings = c(5,1,2,6,3)
)
# broader occurrence dataset
fake_spp_occurrences <- data.frame(
species = rep(c("a","b","c","d",'f'),each=20), # notice spp "f" - not all species are the same between datasets
latitude = runif(100, min = 0, max = 80),
longitude = runif(100, min=-90, max = -55)
)
# so I know to find one species min, i could do this:
min(fake_spp_occurrences$latitude[fake_spp_occurrences$species == "a"]),
# but I want to do that in a mutate()
# this was my failed attempt:
fake_spp_df %>%
mutate(lowest_lat = min(fake_spp_occurrences$latitude[fake_spp_occurrences$species == species])
)
想要的结果:
> fake_spp_df
species sightings lowest_lat max_lat median_lat
1 a 5 1.7 etc...
2 b 1 5.3
3 c 2 2.2
4 d 6 4.3
5 e 3 NA
认为这也可以通过某种连接或合并来完成,但我不确定。
谢谢!
【问题讨论】:
标签: r join merge tidyverse data-cleaning