使用R,对于不平衡面板,计算在给定年份中添加的新个体和已丢弃的旧个体的数量

pod7payv  于 2023-05-20  发布在  其他
关注(0)|答案(1)|浏览(126)

我有不平衡的面板数据,想知道每年有多少新的个体被添加,有多少旧的个体被删除。例如,使用以下数据

Year <- c(2009, 2009, 2009, 2010, 2010, 2010)
ID <- c("A", "A", "B", "B", "D", "C")

df <- data.frame(Year, ID)

我想获得:
| 年份|添加数量|删除数量|
| --------------|--------------|--------------|
| 2009年|2|不适用|
| 2010年|2| 1|

structure(list(Year = c(2009, 2010), `Nr. Added` = c(2, 2), `Nr. Removed` = c(NA, 
1)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, 
-2L))

下面的代码似乎可以识别全新的个体,但不能识别已经消失和重新出现的个体。我也不明白我怎么能概括它来确定在给定的一年中被丢弃的个人

y <- split(df$, arranged_data$year)

z<- data.frame(Year = names(y), nNew =
             diff(lengths(Reduce(union, y, NULL, accumulate = TRUE))))
dw1jzc5e

dw1jzc5e1#

这是一个有点麻烦,但工作:

# Group the data by year and store the unique individual identifiers for each year
    grouped_data <- with(df, split(ID, year))

    # Get the sorted unique years
    years <- sort(unique(df$year))

    # Initialize vectors to store the results
    individuals_added <- numeric(length(years))
    individuals_removed <- numeric(length(years))

    # Calculate individuals added and removed for each year 
    for (i in 2:length(years)) {
    individuals_previous_year <- grouped_data[[i-1]]
    individuals_current_year <- grouped_data[[i]]  
    individuals_added[i] <- sum(!individuals_current_year %in% individuals_previous_year)
    individuals_removed[i] <- sum(!individuals_previous_year %in%  individuals_current_year)}

    # Create a data frame with the results
    df <- data.frame(year = years[-1], individuals_added = individuals_added[-1], individuals_removed = individuals_removed[-1])

相关问题