0

For a sample dataframe:

df <- structure(list(id = 1:25, region.1 = structure(c(1L, 1L, 1L, 
                                                        1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 
                                                        4L, 4L, 4L, 4L, 4L, 4L), .Label = c("AT1", "AT2", "AT3", "AT4"
                                                        ), class = "factor"), gndr = c(0L, 1L, 0L, 0L, 0L, 1L, 0L, 1L, 
                                                                                       1L, 1L, 0L, 0L, 1L, 1L, 0L, 1L, 0L, 0L, 1L, 1L, 0L, 0L, 0L, 1L, 
                                                                                       1L), PoorHealth = c(0L, 1L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 0L, 0L, 
                                                                                                           0L, 1L, 0L, 1L, 0L, 1L, 0L, 1L, 1L, 0L, 0L, 1L, 0L, 1L), weight = c(0.3, 
                                                                                                                                                                               1.6, 2.5, 3.5, 0.2, 0.2, 0.2, 0.6, 0.15, 0.25, 1.36, 1, 1, 1, 
                                                                                                                                                                               0.1, 0.2, 0.3, 0.3, 0.3, 0.4, 0.3, 1, 1.4, 1.3, 0.4)), .Names = c("id", 
                                                                                                                                                                                                                                                 "region.1", "gndr", "PoorHealth", "weight"), class = c("data.table", 
                                                                                                                                                                                                                                                                                                        "data.frame"), row.names = c(NA, -25L))

I wish to create a summary data table (using data.table) using the code:

variable.table_1 <- setDT(df)[,.(.N,result=sum((PoorHealth==1)/.N)*100),
                             by=region.1]

However my original data is from a survey and I therefore have a design and population weight which I have multiplied together (following the guidance from the survey, and have called this variable 'weight').

How do I apply an appropriate weighting of my 'result' variable in variable.table_1?

Perhaps I have to use the survey package? Looking here seems to adjust I have to first run my dataframe through the survey package...

library(survey)
df.w <- svydesign(id = ~1, data = df, weights = df$weight)

... but I am unsure how I incorporate the results into my summary data table.

Many thanks in advance.

KT_1
  • 8,194
  • 15
  • 56
  • 68

1 Answers1

1

Perhaps you can use the weighted.mean function

variable.table_1 <- setDT(df)[,.(.N, result = weighted.mean((PoorHealth==1),
                       w = weight)*100), by = region.1]

In your example you could also simply use mean instead of sum in combination wiht /.N.

mtoto
  • 23,919
  • 4
  • 58
  • 71