我有一个数据集,它包含时间、t1和t2的树度量。这些树是根据州、县、地块和树数来识别的。有些树木已经死亡,有些在t1和t2之间的时间间隔内生长。
State County Plot Tree Meas_yr
1 9 1 1 t1
1 9 1 2 t1
1 9 1 3 t1
1 9 1 1 t2
1 9 1 2 t2
1 9 1 4 t2
1 9 1 5 t2 我正试图将这些树、新生长的树木与t1和t2中的树木以及在t2中死去的树木分开。我正在尝试创建如下所示的东西。
State County Plot Tree Meas_yr tree_survival
1 9 1 1 t1 1
1 9 1 2 t1 1
1 9 1 3 t1 0
1 9 1 1 t2 1
1 9 1 2 t2 1我真的很感谢你的帮助。提前谢谢。
发布于 2022-09-16 01:39:34
你可以试试这个-
library(dplyr)
df %>%
group_by(State, County, Plot, Tree) %>%
dplyr::filter(n() > 1 | Meas_yr == "t1") %>%
mutate(tree_survival = as.integer(n() > 1)) %>%
ungroup()
# State County Plot Tree Meas_yr tree_survival
# <int> <int> <int> <int> <chr> <int>
#1 1 9 1 1 t1 1
#2 1 9 1 2 t1 1
#3 1 9 1 3 t1 0
#4 1 9 1 1 t2 1
#5 1 9 1 2 t2 1这将保留"t1“年的所有行以及这两个年份中都存在的树。如果树同时存在于年份或0中,则为tree_survival分配1。
数据
df <- structure(list(State = c(1L, 1L, 1L, 1L, 1L, 1L, 1L), County = c(9L,
9L, 9L, 9L, 9L, 9L, 9L), Plot = c(1L, 1L, 1L, 1L, 1L, 1L, 1L),
Tree = c(1L, 2L, 3L, 1L, 2L, 4L, 5L), Meas_yr = c("t1", "t1",
"t1", "t2", "t2", "t2", "t2")), class = "data.frame", row.names = c(NA, -7L))发布于 2022-09-16 01:50:25
library(tidyverse)
data <- tribble(
~State, ~County, ~Plot, ~Tree, ~Meas_yr,
1, 9, 1, 1, 't1',
1, 9, 1, 2, 't1',
1, 9, 1, 3, 't1',
1, 9, 1, 1, 't2',
1, 9, 1, 2, 't2',
1, 9, 1, 4, 't2',
1, 9, 1, 5, 't2'
)
new_df <- data %>%
group_by(Tree) %>%
mutate(tree_survival = n() - 1) %>%
ungroup() %>%
filter(!(Meas_yr == 't2' & tree_survival == 0))
## A tibble: 5 x 6
#State County Plot Tree Meas_yr tree_survival
#<dbl> <dbl> <dbl> <dbl> <chr> <dbl>
# 1 1 9 1 1 t1 1
#2 1 9 1 2 t1 1
#3 1 9 1 3 t1 0
#4 1 9 1 1 t2 1
#5 1 9 1 2 t2 1https://stackoverflow.com/questions/73738800
复制相似问题