Reputation: 5
I have a list of several NUTS2
codes for the years 1984-1986. I am trying to get the average GDP for each NUTS2
region over the course of these 3 years.
Below is the code I tried to achieve this with - unfortunately, I always retrieve an error with my last line, saying that "pull" is not an applicable method for this class.
NUTS_CODE NUTS_LEVEL SCENARIO_ID REF_YEAR IND_VALUE NUTS_C
837 BE10 2 1 1984 2.307e+10 BE
838 BE21 2 1 1984 2.195e+10 BE
839 BE22 2 1 1984 6.330e+09 BE
840 BE23 2 1 1984 1.340e+10 BE
841 BE24 2 1 1984 9.430e+09 BE
842 BE25 2 1 1984 1.093e+10 BE
regions <- unique(as.character(gdp_nuts2_member_1984to1986$NUTS_CODE))
data84_86 <- regions
data84_86 <-cbind(data84_86, rep(as.numeric(NA), length(regions)))
colnames(data84_86) <- c("regions","values")
for(i in 1:nrow(data84_86))
{
data84_86[i,2]<-mean(pull((gdp_nuts2_member_1984to1986%>%filter(NUTS_CODE == regions[i]))[,"IND_VALUE"]))
}
Error in UseMethod("pull") : no applicable method for 'pull' applied to an object of class "c('double', 'numeric')"
Upvotes: 0
Views: 8452
Reputation: 11981
the pull
function works a little bit differently:
library(dplyr)
data84_86[i,2] <- mean(pull(gdp_nuts2_member_1984to1986 %>%
filter(NUTS_CODE == regions[i]), IND_VALUE))
Your code becomes even more readable if you write it like this:
data84_86[i,2] <- gdp_nuts2_member_1984to1986 %>%
filter(NUTS_CODE == regions[i]) %>%
pull(IND_VALUE) %>%
mean()
Also, the same result can be obtained like this:
gdp_nuts2_member_1984to1986 %>%
group_by(NUTS_CODE) %>%
summarise(values = mean(IND_VALUE))
This way is even easier to read and computationally more efficient.
Upvotes: 1