首页 > 解决方案 > R:我可以让 rename 接受一个函数作为新的 varname

问题描述

我有一个数据集,想通过教育和种族创建一个时间序列。但是,我正在努力让 rename() 接受一个命令,而不仅仅是一个字符串作为新的变量名。

我的代码:

#libraries
install.packages(c("tidyverse", "spatstat"))
lapply(c("tidyverse", "spatstat"), require, character.only = TRUE)

#calculate weighted medians by race and education
wmedians <- lapply(data[,c("race", "education")],function(vars){
  data %>%
    group_by((vars), year) %>%
    summarize(w_median = weighted.median(wealth, weight))%>%
    rename(colnames(vars) = "(vars)")
})

这给了我以下错误:

错误:意外'=' in:“summary(w_median = weighted.median(wealth, weight))%>% rename(colnames(vars) =

期望 的输出:目前我得到一个包含两个小标题的列表,其中第一列的列名是"(vars)". 相反,我希望列名"race"位于第一个小标题和"education"第二个小标题中。

我尝试了一下,我的猜测是重命名函数通常不接受任何函数(例如 colnames())作为新变量名。你知道如何解决这个问题吗?

我的数据样本:

structure(list(year = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 
5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 
8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 
9L, 9L, 9L, 9L, 9L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 
10L, 10L, 10L), .Label = c("1989", "1992", "1995", "1998", "2001", 
"2004", "2007", "2010", "2013", "2016"), class = "factor"), weight = c(9084.9691295, 
1571.9511258, 191.75635451, 204.62890325, 204.62890325, 20.462890325, 
10.412082059, 144.25723032, 66.170395167, 17169.253056, 5240.2917738, 
768.75688855, 152.72765752, 166.52285228, 4165.0038712, 12.696579164, 
1.3158341152, 3574.4768327, 18.640983311, 687.040202, 6738.6966881, 
0.9648849583, 2.9597366608, 20.019564258, 895.51359665, 1.3152534108, 
3244.207427, 2.0000789024, 750.95122778, 1.6580375994, 4007.7581965, 
4.7569235917, 180.73948443, 237.26008744, 2.8105880617, 2.8105880617, 
1.7964957199, 4883.711226, 17.268444467, 2.9783310762, 354.15138196, 
162.00933944, 1.6450475811, 1.3755398392, 4174.6347012, 44.17020127, 
4987.2079388, 1.3755398392, 18.01293584, 3.3426730968, 1.5455142055, 
904.20169275, 12.578831203, 10051.580218, 162.70814346, 2.1257090517, 
2.1257090517, 1027.7013368, 8166.4587927, 1.7239086827, 2.1374243666, 
0.901741906, 2.9900010571, 33.443685091, 12913.631224, 5973.1098594, 
9527.6211412, 2.2331957715, 376.47055359, 2.9488054663, 2.9488054663, 
2.8570458091, 8.0625166988, 4.7867036342, 923.0539464, 2.7381019933, 
5800.0572063, 4.3379657179, 0.8195417131, 6108.0937784, 23.232719795, 
121.97520298, 1684.0365357, 4.7867036342, 4.8109344834, 479.31438165, 
4.8109344834, 3.7209489469, 5257.7592767, 3220.9438379, 3.1738803883, 
3.8910375552, 3.7209489469, 30.231850875, 3277.3748665, 32.237421329, 
2.7966498146, 3208.415157, 34.375315295, 30.200771547, 31.811971048, 
20.701306688, 2.7966498146, 31.968899323, 33.280487562, 2548.6399138, 
32.94638396, 2.7966498146, 2.6033164134, 30.815250688, 23.702590485, 
31.465956118, 29.46116036, 760.36545895, 8.3114397117, 23.702590485, 
4.6504256805, 3204.2440292, 6.8002099257, 35.813850525), race = structure(c(1L, 
1L, 1L, 4L, 4L, 4L, 4L, 4L, 4L, 1L, 1L, 1L, 1L, 1L, 1L, 4L, 4L, 
1L, 4L, 1L, 1L, 4L, 4L, 4L, 1L, 4L, 1L, 4L, 1L, 4L, 1L, 4L, 1L, 
1L, 4L, 4L, 4L, 1L, 4L, 4L, 1L, 1L, 4L, 1L, 1L, 4L, 1L, 4L, 4L, 
4L, 4L, 1L, 4L, 1L, 1L, 4L, 1L, 1L, 1L, 4L, 4L, 4L, 1L, 1L, 1L, 
1L, 1L, 4L, 1L, 4L, 4L, 4L, 4L, 4L, 1L, 4L, 1L, 4L, 4L, 1L, 1L, 
1L, 1L, 4L, 4L, 1L, 4L, 4L, 1L, 1L, 1L, 1L, 4L, 4L, 1L, 4L, 4L, 
1L, 1L, 4L, 1L, 4L, 4L, 4L, 1L, 1L, 1L, 4L, 4L, 1L, 1L, 1L, 4L, 
1L, 4L, 4L, 4L, 1L, 4L, 1L), .Label = c("black", "Hispanic", 
"other", "white"), class = "factor"), education = structure(c(2L, 
1L, 1L, 2L, 1L, 1L, 2L, 2L, 1L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 1L, 
2L, 2L, 1L, 2L, 1L, 1L, 2L, 2L, 1L, 2L, 2L, 1L, 2L, 2L, 2L, 1L, 
1L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 
1L, 1L, 1L, 2L, 2L, 1L, 2L, 1L, 2L, 2L, 1L, 2L, 1L, 1L, 1L, 2L, 
2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 2L, 1L, 2L, 1L, 
1L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 
2L, 2L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
2L, 1L, 2L, 2L, 2L, 2L, 2L), .Label = c("college degree", "no college", 
"some college"), class = "factor"), wealth = c(370932.98, 10170000.94, 
12598660.39, 114293258.81, 211275067.86, 290691670.17, 85726008.16, 
230065771.49, 246480115.73, 349587.55, 378694.96, 3164512.87, 
8495442.34, 6020105.92, 505133.05, 367073411.82, 2928346179.67, 
533643.21, 554996993.66, 5648836.48, 392098.63, 692454429.71, 
872798466.66, 352917443.83, 798505.65, 1250534235.01, 645694.94, 
570963643.71, 10983328.56, 360732249.01, 717840.77, 365513260.49, 
7204246.71, 8901952.1, 473348324.51, 686068914.54, 566191645.55, 
451622.81, 376484717.9, 702785331.9, 6677625.87, 5826581.94, 
538339875.62, 81126854.16, 1073395.38, 595512233.49, 769008.48, 
363748981.28, 203466108.48, 801021687.19, 744505545.07, 5219227.59, 
132716087.05, 813859.01, 4429075.76, 376023173.93, 418753292.21, 
591392.47, 986060.58, 740193054.24, 305729499.91, 815008777.34, 
292998224.45, 25586473.36, 1482007.57, 832890.03, 1431390.64, 
465587944.43, 31578347.08, 771207206.27, 774386788.46, 283388639.66, 
491131539.34, 1106105605.16, 15792325.59, 448260665.36, 1598512.35, 
256789830.23, 1327614423.44, 1034918.02, 44314918.71, 47310703.56, 
1174945.19, 1050196871.69, 791040687.75, 40226229.67, 1193945180.43, 
882118783.02, 865693.49, 598203.92, 94409003.49, 321989895.39, 
611845894.05, 414941965.04, 1697121.78, 192234467.65, 1200517207.56, 
1522890.02, 42378401.16, 269560657.85, 49377539.54, 450366559.75, 
1362371354.42, 254034284.01, 65726116.22, 840350.29, 87111742.24, 
1116803883.14, 1703800000, 248761000, 104962000, 111565400, 1224715000, 
1405000, 1196478000, 981045000, 535052000, 2077700, 444554200, 
41907500)), row.names = c(NA, -120L), groups = structure(list(
    year = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 
    3L, 3L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 7L, 
    7L, 7L, 7L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 10L, 10L, 10L, 
    10L), .Label = c("1989", "1992", "1995", "1998", "2001", 
    "2004", "2007", "2010", "2013", "2016"), class = "factor"), 
    education = structure(c(1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 
    1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 
    1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 
    2L), .Label = c("college degree", "no college", "some college"
    ), class = "factor"), race = structure(c(1L, 4L, 1L, 4L, 
    1L, 4L, 1L, 4L, 1L, 4L, 1L, 4L, 1L, 4L, 1L, 4L, 1L, 4L, 1L, 
    4L, 1L, 4L, 1L, 4L, 1L, 4L, 1L, 4L, 1L, 4L, 1L, 4L, 1L, 4L, 
    1L, 4L, 1L, 4L, 1L, 4L), .Label = c("black", "Hispanic", 
    "other", "white"), class = "factor"), .rows = structure(list(
        c(2L, 3L, 12L), c(5L, 6L, 9L), c(1L, 10L, 11L), c(4L, 
        7L, 8L), c(13L, 14L, 20L), c(17L, 22L, 23L), c(15L, 18L, 
        21L), c(16L, 19L, 24L), c(29L, 33L, 34L), c(26L, 35L, 
        36L), c(25L, 27L, 31L), c(28L, 30L, 32L), c(41L, 42L, 
        44L), c(37L, 40L, 43L), c(38L, 45L, 47L), c(39L, 46L, 
        48L), c(52L, 55L, 57L), c(50L, 51L, 60L), c(54L, 58L, 
        59L), c(49L, 53L, 56L), c(63L, 64L, 69L), c(62L, 70L, 
        71L), 65:67, c(61L, 68L, 72L), c(75L, 81L, 82L), c(74L, 
        79L, 84L), c(77L, 80L, 83L), c(73L, 76L, 78L), c(86L, 
        91L, 92L), c(85L, 87L, 88L), c(89L, 90L, 95L), c(93L, 
        94L, 96L), c(101L, 105L, 107L), c(97L, 103L, 108L), c(98L, 
        99L, 106L), c(100L, 102L, 104L), 110:112, c(109L, 113L, 
        115L), c(114L, 118L, 120L), c(116L, 117L, 119L)), ptype = integer(0), class = c("vctrs_list_of", 
    "vctrs_vctr", "list"))), row.names = c(NA, -40L), class = c("tbl_df", 
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"))

提前非常感谢!

标签: rdplyrrename

解决方案


将列名传入lapply并用于.data引用它们,group_by因此您根本不需要rename,因为列名将被保留。

library(dplyr)
library(spatstat)

lapply(c("race", "education"),function(vars){
  data %>%
    group_by(.data[[vars]], year) %>%
    summarize(w_median = weighted.median(wealth, weight))
}) -> result

result

推荐阅读