Advertisements Suppose you have the following dataframe: df <- data.frame(year=c(rep(2010,12),rep(2011,12),rep(2012,12)), country=c(rep("DEU",4),rep("ITA",4),rep("USA",4), rep("DEU",4),rep("ITA",4),rep("USA",4), rep("DEU",4),rep("ITA",4),rep("USA",4)), industry=c(rep(1:4,9)), stock1=c(rep(0,24),0,0,2,4,1,0,1,2,3,3,3,5), stock2=c(rep(0,24),0,3,3,4,5,0,1,1,2,2,2,5)) and you want to get the following outcome: df2 <- data.frame(year=c(rep(2010,12),rep(2011,12),rep(2012,12)), country=c(rep("DEU",4),rep("ITA",4),rep("USA",4), rep("DEU",4),rep("ITA",4),rep("USA",4), rep("DEU",4),rep("ITA",4),rep("USA",4)), industry=c(rep(1:4,9)), stock1=c(rep(NA,24),0,0,2,4,1,0,1,2,3,3,3,5), stock2=c(rep(NA,24),0,3,3,4,5,0,1,1,2,2,2,5)) The concept is that if, for a particular year, a specific country reports zeros in stock2 across ALL industries, then those zeros… Read More Fill NAs when you have zeros in your dataset