I have the following example survey dataset:
df <- data.frame(sex = c(1, 1, 2, 2, 1, 2, 2, 2, 1, 2),
age = c(15, 40, 97, 25, 99, 65, 20, 99, 39, 48),
nationality= c(1, 3, 1, 2, 4, 97, 2, 2, 2, 99),
employment = c(2, 1, 99, 1, 1, 1, 1, 1, 2, 2),
income = c(-1, 2500, 999997, 10000, 65000, 999998, 999999, 15000, -1, -1),
weight = c(100, 20, 400, 300, 50, 50, 80, 250, 100, 100))
The following list contains selected variables that I want to use in a for loop:
list <- list(age = df$age, employment = df$employment, income = df$income)
I want to loop over the list of selected variables in the dataframe, and for each variable in the list apply a filter (condition) and get a weighted frequency table from the filtered data. In pseudocode this is what I want to do:
for i in list {
filter(i >= 1 & i <= max(i)-2 %>%
weighted frequency of var i based on 'weight'
}
I have tried many ways to do this in R but I still can’t figure out how. The last time I used this:
library(dplyr)
library(expss)
for (i in list){
filter(i > 1 & i < max(i))-2 %>%
fre(i, weight = df$weight)
}
But I get this error message:
Error in UseMethod("filter_") :
no applicable method for 'filter_' applied to an object of class "logical"
I need to figure out how to do it because I need to loop over a list of 256 variables.
The results must be:
library(dplyr)
library(expss)
age: <br />
F <- df %>% filter(age >= 1 & age < 97)
fre(F$age, weight = F$weight)
| F$age | Count | Valid percent | Percent | Responses, % | Cumulative responses, % |
| ------ | ----- | ------------- | ------- | ------------ | ----------------------- |
| 15 | 100 | 13.3 | 13.3 | 13.3 | 13.3 |
| 20 | 80 | 10.7 | 10.7 | 10.7 | 24.0 |
| 25 | 300 | 40.0 | 40.0 | 40.0 | 64.0 |
| 39 | 100 | 13.3 | 13.3 | 13.3 | 77.3 |
| 40 | 20 | 2.7 | 2.7 | 2.7 | 80.0 |
| 48 | 100 | 13.3 | 13.3 | 13.3 | 93.3 |
| 65 | 50 | 6.7 | 6.7 | 6.7 | 100.0 |
| #Total | 750 | 100.0 | 100.0 | 100.0 | |
| <NA> | 0 | | 0.0 | | |
employment: <br />
F <- df %>% filter(employment >= 1 & employment < 97)
fre(F$employment, weight = F$weight)
| F$employment | Count | Valid percent | Percent | Responses, % | Cumulative responses, % |
| ------------ | ----- | ------------- | ------- | ------------ | ----------------------- |
| 1 | 750 | 71.4 | 71.4 | 71.4 | 71.4 |
| 2 | 300 | 28.6 | 28.6 | 28.6 | 100.0 |
| #Total | 1050 | 100.0 | 100.0 | 100.0 | |
| <NA> | 0 | | 0.0 | | |
income: <br />
F <- df %>% filter(income >= 1 & income < 999997)
fre(F$income, weight = F$weight)
| F$income | Count | Valid percent | Percent | Responses, % | Cumulative responses, % |
| -------- | ----- | ------------- | ------- | ------------ | ----------------------- |
| 2500 | 20 | 3.2 | 3.2 | 3.2 | 3.2 |
| 10000 | 300 | 48.4 | 48.4 | 48.4 | 51.6 |
| 15000 | 250 | 40.3 | 40.3 | 40.3 | 91.9 |
| 65000 | 50 | 8.1 | 8.1 | 8.1 | 100.0 |
| #Total | 620 | 100.0 | 100.0 | 100.0 | |
| <NA> | 0 | | 0.0 | | |