Reputation: 111
I have a panel dataset, with entries for every country in 5-year intervals (earliest 1960, latest 2000). Each entry has values such as a democracy index, logarithm gdp, etc. I want to find the average of democracy index for each country, over all periods it has entries over. There are some NA values.
An example is
Andorra 1960 NA
Andorra 1965 NA
Andorra 1970 0.50
Andorra 1975 NA
Andorra 1980 NA
Andorra 1985 NA
Andorra 1990 NA
Andorra 1995 1.00
Afghanistan 1960 0.14
and so on.
Each country also has a code value, starting at 1 for Andorra, increasing as you go down the alphabet (so Andorra is 1, Afghanistan is 2, Angola is 3, and so on).
I have looked at other panel data questions but they seem either irrelevant or the code is too complex for me to see if it is relevant. Do you have any recommendations?
Thank you in advance.
Upvotes: 1
Views: 221
Reputation: 886938
We can use aggregate
from base R
to get the mean
of the 'democracy_index' column grouped by the 'country' column
aggregate(democracy_index ~ country, df1, mean, na.rm = TRUE, na.action = NULL)
Upvotes: 2