Search code examples
rdplyrplyr

operating numcolwise on a specified columns


I am working with a large cross country panel data. Here is a sample of my data:

df <- structure(list(country = c("Argentina", "Argentina", "Argentina", 
"Argentina", "Argentina", "Argentina", "Argentina", "Argentina", 
"Argentina", "Argentina", "Argentina", "Argentina", "Argentina", 
"Argentina", "Argentina", "Brazil", "Brazil", "Brazil", "Brazil", 
"Brazil", "Brazil", "Brazil", "Brazil", "Brazil", "Brazil", "Brazil", 
"Brazil", "Brazil", "Brazil", "Brazil"), year = c(1991, 1992, 
1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 
2004, 2005, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 
2000, 2001, 2002, 2003, 2004, 2005), lnunderval = c(-0.942018220566855, 
-0.885848248127534, -0.766349222095516, -0.690487190951407, -0.521023028925771, 
-0.288557433912095, -0.351488637772915, -0.393048184068511, -0.444123691025518, 
-0.512425182981147, -0.541182815398097, 0.379018666505875, 0.291852440172936, 
0.291407056285245, 0.221426753100227, -0.120418577004832, 0.00467960055625634, 
-0.0190735963658737, -0.239570582118898, -0.316748349307701, 
-0.205418347557874, -0.301707274202926, -0.346946676711871, -0.0528811487098006, 
-0.178001370772517, -0.0404491572081528, 0.0898307782259906, 
0.0835291098039626, 0.0349739055576117, -0.187321483795299), 
    manu_GDP = c(24.3864490932335, 21.8591315586603, 18.2399115325496, 
    17.8190917106899, 17.2467521148076, 17.5357232920479, 18.227905749866, 
    17.8379584760908, 16.9615250614589, 16.4942719439838, 16.0932258763829, 
    20.347773913878, 22.4867505875749, 18.9370136214371, 18.340415936715, 
    21.8391379495813, 23.3085986320751, 26.0497364463813, 23.7212337008806, 
    14.5422791544751, 13.0671912367218, 13.0186253732125, 12.1551371940101, 
    12.3085333305115, 13.134659593552, 13.0895379354001, 12.3569626673735, 
    14.4507645630532, 15.0995301563871, 14.7382811342998), income = c("Upper middle income", 
    "Upper middle income", "Upper middle income", "Upper middle income", 
    "Upper middle income", "Upper middle income", "Upper middle income", 
    "Upper middle income", "Upper middle income", "Upper middle income", 
    "Upper middle income", "Upper middle income", "Upper middle income", 
    "Upper middle income", "Upper middle income", "Upper middle income", 
    "Upper middle income", "Upper middle income", "Upper middle income", 
    "Upper middle income", "Upper middle income", "Upper middle income", 
    "Upper middle income", "Upper middle income", "Upper middle income", 
    "Upper middle income", "Upper middle income", "Upper middle income", 
    "Upper middle income", "Upper middle income"), period = structure(c(1L, 
    1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 1L, 
    1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L), .Label = c("(1990,1995]", 
    "(1995,2000]", "(2000,2005]"), class = "factor")), row.names = c(NA, 
-30L), class = c("tbl_df", "tbl", "data.frame"))

I created five-year non-overlapping averages of my variables using cut and ddply functions as below.

df$period <- cut(df$year, seq(1990, 2005, 5)) #this periodizes data
df <- ddply(df, .(country, period), numcolwise(mean)) 

The problem with this code is that the non-numeric column named income is lost. I've tried the following but it did not work.

df <- ddply(df, .(country, period), numcolwise(mean,.(lnunderval, manu_GDP))) 
Error in mean.default(X[[i]], ...) : 'trim' must be numeric of length one

I would like to final dataset to contain non-numeric columns that are not averaged. Is there a way of applying the numcolwise function on a specified set of columns?

I would like the final output to look like this:

structure(list(country = c("Argentina", "Argentina", "Argentina", 
"Brazil", "Brazil", "Brazil"), period = structure(c(1L, 2L, 3L, 
1L, 2L, 3L), .Label = c("(1990,1995]", "(1995,2000]", "(2000,2005]"
), class = "factor"), year = c(1993, 1998, 2003, 1993, 1998, 
2003), lnunderval = c(-0.761145182133417, -0.397928625952037, 
0.128504420133237, -0.13822630084821, -0.216990963590998, -0.00388736948317731
), manu_GDP = c(19.9102672019882, 17.4114769046895, 19.2410359871976, 
21.8921971766787, 12.7368293456016, 13.9470152913027), income = c("Upper middle income", 
"Upper middle income", "Upper middle income", "Upper middle income", 
"Upper middle income", "Upper middle income")), class = "data.frame", row.names = c(NA, 
-6L))

Solution

  • We may use dplyr which is more flexible with across to summarise multiple blocks of columns with different functions

    library(dplyr)
    df %>%
      group_by(country, period) %>%
      summarise(year = last(year), income = list(unique(income[!is.na(income)])), 
        across(c(lnunderval, manu_GDP), mean), .groups = 'drop')