Transitioning from SAS to R -- How to efficiently repeat the same task in R

Viewed 120

I have 24 variables that I need to get the frequency for when combined with another variables. In SAS it will be a simple macro :

%macro results (house_color,variable); 
proc freq data = all_houses; 
table variable*majority_&house_color. / missing out= &varibale._by_&house_color.; 
run; 
% mend; 

%results( house_color = purple, variable=pool_or_no_pool) ; 
%results (house_color = blue, variable = upb);

I tried creating a function in R using a code provided in a different question I asked earlier:

results <- function(x,y,d,z){
  freqs <- freqlist(table(z[c(x,y)],useNA = "always"))
  freq_df <- as.data.frame(freqs) %>% select(1:3)
  colnames(freq_df)[3] <- d
  freq_df 
} 

where x is the first variable, y is the second variable, d is what I want the Freq columns to be renamed as and z is the dataset. However, when I get the result from the function and I try to use cbind to have all of the information consolidated it is giving me the following error:

Error in data.frame(..., check.names = FALSE) : arguments imply differing number of rows: 10, 11

I was wondering if there any simpler way to obtain the frequency table of these 24 variables and either stack or cbind the information without creating too much NA's. I have use rbind.fill but the dataframe produce is messy and has a lot of NA's.

Thank you in advance for the help.

0 Answers
Related