0

using this below code I am trying to weight each year in each iteration based on column "weight". using wtd.table I have the following code:

wtd.table(result$`it 1`$`year 5`$age,result$`it 1`$`year 5`$gender ,result$`it 1`$`year 5`$district, weights =result$`it 1`$`year 5`$weight )

This output only provides age and gender weighted. It does not also include district. based on some purposes I have used count(var1, var2, var3 , wt = wt) like this:

count(result$`it 1`$`year 5`$age,result$`it 1`$`year 5`$gender,
result$`it 1`$`year 5`$age,
wt = result$`it 1`$`year 5`$weight)

but I got this error: Error in UseMethod("count") : no applicable method for 'count' applied to an object of class "c('double', 'numeric')". how can I fix that?

here is my code:

iter1 <- list(year1 =data.frame(age=c(10,11,12,13), district=c(1,2,3,4),gender=c(1,2,2,1)
,weight=c(12.2,11.3,11.2,10.1)),
year2 =data.frame(age=c(10,11,12,13), district=c(1,2,3,4),gender=c(1,2,2,1),weight=c(12.2,11.3,11.2,10.1)))

iter2 <- list(year1 =data.frame(age=c(10,11,12,13), district=c(1,2,3,4),gender=c(2,2,1,1)
,weight=c(12.2,11.3,11.2,10.1)),
year2 =data.frame(age=c(10,11,12,13), district=c(1,2,3,4),gender=c(2,2,1,1),weight=c(12.2,11.3,11.2,10.1)))

df <- list(iter1 = iter1, iter2 = iter2)

my expected output is like df1

df1<-

district   age    gender  freq   
1          10       1     12.2
1          11       1      0.0 
1          12       1      0.0
1          13       1     10.1
1          10       2      0.0
1          11       2     11.3
1          12       2     11.2
1          13       2      0.0

mehmo
  • 409
  • 2
  • 8

1 Answers1

1

You can combine the nested list in one dataframe and use complete to fill the missing observations of weight with 0.

library(tidyverse)

map_df(df, ~bind_rows(.x, .id = 'year'), .id = 'iter') %>%
  complete(iter, year, age, district, gender, fill = list(weight = 0))
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
  • thanks @ Ronak shah. what if I want to have just specific year and iteration? – mehmo Jun 01 '21 at 11:34
  • You can `filter` the values that you want. `%>% filter(iter == 'iter1', year == 'year1')` – Ronak Shah Jun 01 '21 at 11:39
  • I got this error no applicable method for 'filter' applied to an object of class "list" – mehmo Jun 01 '21 at 11:50
  • @mehmo You need to add `filter` step after `complete`. I don't get any such error when using `map_df(df, ~bind_rows(.x, .id = 'year'), .id = 'iter') %>% complete(iter, year, age, district, gender, fill = list(weight = 0)) %>% dplyr::filter(iter == 'iter1', year == 'year1')` – Ronak Shah Jun 01 '21 at 11:51
  • actually it works but produces 0 observation – mehmo Jun 01 '21 at 11:57
  • @mehmo Using my above code (in comment) on the data that you have shared I get 32 rows. – Ronak Shah Jun 01 '21 at 11:59
  • thank you so much and one more question, can I have mean of iterations in a data frame and in different confidence interval? – mehmo Jun 01 '21 at 12:03
  • I am not sure if I completely understand your question but mean of iterations can be calculated using `group_by` and `mean`. This post might help https://stackoverflow.com/questions/11562656/calculate-the-mean-by-group – Ronak Shah Jun 01 '21 at 12:31