r语言 - 如何在For循环中索引多个对象/变量



我有一个由三列组成的数据:

户号,产品编号(H14aq2),值。

我有大约7000行(家庭id),可以分为12个地区和160个产品。HH id可以出现多次,因为它们使用多个产品。我的目标是对每个产品的家庭价值求和,这样我就得到了一个地区范围内的产品价值总和。我知道如何手动实现这一点,但我想使用循环,因为我将为多个数据集这样做。

这是我当前的代码。这实际上运行时没有错误,显示156次迭代,但是当我查看total_values_05对象时,只附加了一个额外的向量,val_i。

for(i in 105:161){

total_val_i <- cons_05 %>% 
filter(H14aq2 == i) %>% 
group_by(Districtn05) %>% 
summarise(val_i = sum(total_val_yr)) %>% 
ungroup()

total_values_05 <- total_values_05 %>% 
left_join(total_val_i)
rm(total_val_i)

}

有161个产品(用变量H14aq2从101到161进行索引)。在此循环之前,我创建了对象total_values_05,出于其他原因,我在其中处理产品101到104。

在每次迭代中,我想过滤单个产品,对包含值的total_val_yr变量求和,然后将新向量val_i附加到现有对象total_values_05上。最终,我想要一个结构如下的对象:

<表类>区val_101val_102val_103tbody><<tr>第一个行行行第二行行行

您可以按多列分组,然后将汇总的结果转为宽格式,如下所示:

library(tidyverse)
data <- structure(list(
Hhid = structure(c(
"1033000301", "1033000301",
"1033000301", "1033000301", "1033000301", "1033000301"
), label = "Unique hh identifier across panel waves", format.stata = "%-10s"),
Districtn05 = structure(c(
"Kiboga", "Kiboga", "Kiboga", "Kiboga",
"Kiboga", "Kiboga"
), label = "District name as in 2005/06", format.stata = "%-13s"),
H14aq2 = structure(c(150, 135, 140, 136, 112, 103), label = "Consumption item code", format.stata = "%16.0g", labels = c(
Matooke = 101,
Matooke = 102, Matooke = 103, Matooke = 104, `Sweet potatoes fresh` = 105,
`Sweet potatoes dry` = 106, `Cassava fresh` = 107, `Cassava dry/flour` = 108,
`Irish potatoes` = 109, Rice = 110, `Maize grains` = 111,
`Maize cobs` = 112, `Maize flour` = 113, Bread = 114, Millet = 115,
Sorghum = 116, Beef = 117, Pork = 118, `Goat meat` = 119,
`Other meat` = 120, Chicken = 121, `Fresh fish` = 122, `Dry/smoked fish` = 123,
Eggs = 124, `Fresh milk` = 125, `Infant formula foods` = 126,
`Cooking oil` = 127, Ghee = 128, `Margarine,butter` = 129,
`Passion fruits` = 130, `Sweet bananas` = 131, Mangoes = 132,
Oranges = 133, `Other fruits` = 134, Onions = 135, Tomatoes = 136,
Cabbages = 137, Dodo = 138, `Other vegetables` = 139, `Beans fresh` = 140,
`Beans dry` = 141, `Ground nuts in shell` = 142, `Ground nuts shelled` = 143,
`Ground nuts pounded` = 144, Peas = 145, Simsim = 146, Sugar = 147,
Coffee = 148, Tea = 149, Salt = 150, Soda = 151, Beer = 152,
`Other alcoholic drinks` = 153, `Other drinks` = 154, Cigarettes = 155,
`Other tobbaco` = 156, `Expenditure in restaurants on food` = 157,
`Expenditure in restaurants on soda` = 158, `Expenditure in restaurants on beer` = 159,
`Other juice` = 160, `Other foods` = 161
), class = c(
"haven_labelled",
"vctrs_vctr", "double"
)), total_val_yr = c(
3250, 10400, 156000,
10400, 260000, 312000
)
), row.names = c(NA, -6L), class = c(
"tbl_df",
"tbl", "data.frame"
))

data %>%
group_by(Districtn05, H14aq2) %>%
summarise(total_val_yr = sum(total_val_yr)) %>%
select(total_val_yr, H14aq2) %>%
pivot_wider(names_from = H14aq2, values_from = total_val_yr, names_prefix = "val_")
#> `summarise()` has grouped output by 'Districtn05'. You can override using the
#> `.groups` argument.
#> Adding missing grouping variables: `Districtn05`
#> # A tibble: 1 × 7
#> # Groups:   Districtn05 [1]
#>   Districtn05 val_103 val_112 val_135 val_136 val_140 val_150
#>   <chr>         <dbl>   <dbl>   <dbl>   <dbl>   <dbl>   <dbl>
#> 1 Kiboga       312000  260000   10400   10400  156000    3250

在2022-05-25由reprex包(v2.0.0)创建

相关内容

  • 没有找到相关文章

最新更新