在指定的列上操作numcolwise

发布于 2025-02-03 13:05:58 字数 4161 浏览 1 评论 0原文

我正在使用大型越野面板数据。这是我的数据示例:

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"))

我使用剪切和ddply函数如下创建了我的变量的五年非重叠平均值。

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

该代码的问题在于,命名收入的非数字列丢失。我已经尝试了以下操作,但它不起作用。

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

我想最终数据集包含未平均的非数字列。是否有一种方法可以在指定的一组列上应用NumColwise函数?

我希望最终的输出看起来像这样:

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))

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))

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怼怹恏 2025-02-10 13:05:58

我们可以使用dplyr,它在 to 的 多个具有不同功能的列块中更灵活

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')

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')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文