您可以使用dplyr
做到這一點。這裏有一個版本,每年將數非NA每列的值:
# make toy data
set.seed(20912)
df <- data.frame(country = c(rep("A", 5), rep("B", 5)),
year = rep(seq(1995,1999), 2),
var1 = rnorm(10, 100, 20),
var2 = sample(c("lo", "hi", NA), 10, replace = TRUE),
stringsAsFactors = FALSE)
library(dplyr)
# create function for counting non-NA values
noNAsum <- function(x) { sum(!is.na(x)) }
# group the data by year and then apply the function to each column of interest
df %>%
group_by(year) %>%
summarise_each(funs(noNAsum), var1:var2)
# A tibble: 5 × 3
year var1 var2
<int> <int> <int>
1 1995 2 1
2 1996 2 2
3 1997 2 0
4 1998 2 2
5 1999 2 2
如果要使用不同的規則不同的列,您可以擴展調用summarize
以容納,例如:
df %>%
group_by(year) %>%
summarise(var1 = sum(var1 > 100),
var2 = sum(!is.na(var2)))
# A tibble: 5 × 3
year var1 var2
<int> <int> <int>
1 1995 1 1
2 1996 1 2
3 1997 0 0
4 1998 1 2
5 1999 0 2
計數的是什麼? – ulfelder
請添加數據的一個例子。 – Ansjovis86
目前尚不清楚。也許你正在尋找'DT [,list(total = sum(v_23_27!= 99999)),by = list(ID_year,ID_country)]' –