'Problem when creating a weights column in the table

Running regression with panel data on different geographical levels in the US and Euro area with weights that essentially look like this:

lm(log(POP25) ~ log(EMPLOY25), weights = weights, data = data)

The weights are the 2007 observations of POP25 for every grouping. This is the code and data for Europe. (For this dataset I don't experience any trouble.)

require(dplyr)
Data  <- Data  |>
    group_by(NUTS_ID)  |>
    mutate(weights = POP25[TIME==2007])

Data 1:

structure(list(...1 = 1:6, TIME = 2007:2012, NUTS_ID = c("AT", 
"AT", "AT", "AT", "AT", "AT"), NUMBER = c(1L, 1L, 1L, 1L, 1L, 
1L), POP15 = c(5529.1, 5549.3, 5558.5, 5572.1, 5601.1, 5620.8
), POP20 = c(5047.1, 5063.2, 5072.6, 5090, 5127.1, 5151.9), POP25 = c(4544, 
4560.7, 4571.3, 4587.8, 4621.5, 4639), EMPLOY15 = c(3863.6, 3928.7, 
3909.3, 3943.9, 3982.3, 4013.4), EMPLOY20 = c(3676.2, 3737, 3723.8, 
3761.9, 3802.3, 3835), EMPLOY25 = c(3333.5, 3390.4, 3384.7, 3424.6, 
3454.4, 3486.4), weights = c(4544, 4544, 4544, 4544, 4544, 4544
)), class = c("grouped_df", "tbl_df", "tbl", "data.frame"), row.names = c(NA, 
-6L), groups = structure(list(NUTS_ID = "AT", .rows = structure(list(
    1:6), ptype = integer(0), class = c("vctrs_list_of", "vctrs_vctr", 
"list"))), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, 
-1L), .drop = TRUE))

However, I am not able to do the same code on data for US counties.

US_County  <- US_County  |>
    group_by(NAME) |>
    mutate(weights = POP25[year==2007])

Data 2:

structure(list(NAME = c("Ada County, Idaho", "Ada County, Idaho", 
"Ada County, Idaho", "Ada County, Idaho", "Ada County, Idaho", 
"Ada County, Idaho"), GEOID = c(16001, 16001, 16001, 16001, 16001, 
16001), year = c(2007, 2008, 2009, 2010, 2011, 2012), POP25 = c(205888, 
208506, 212770, 212272, 216058, 220856), EMPLOY25 = c(161385, 
160303, 152131, 155292, 155574, 164830), State = c("Idaho", "Idaho", 
"Idaho", "Idaho", "Idaho", "Idaho"), StateID = c(16, 16, 16, 
16, 16, 16)), row.names = c(NA, 6L), class = "data.frame")

When doing it with the last dataset I get this error message that I can´t figure out what means.

Error in `mutate()`:
! Problem while computing
  `weights = POP25[year == 2007]`.
x `weights` must be size 5 or 1,

I don´t know if there is anything wrong with the data. I have tried specifying the class so that everything should be equal across the datasets. I have also tried removing all NA observations, however with no luck.

Am I doing something wrong in my code?

Are there any other ways to do the same?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source