gabriel fernandez
gabriel fernandez

Reputation: 19

R table one variable vs all other from db

I'm trying to generate tables of 1 variable vs all my other variables, with names made from the variables.

My expected output would be as many table as the number of variables, as if I asked for tables for each couple of variable, with names as bro2vOther_var_name:

bro2vagecat

                  11   13   15
  Less frequent 2018 1827 1630
  Twice a day   4276 4782 4194

bro2vsex

                 Boy Girl
  Less frequent 3342 2133
  Twice a day   6010 7242


bro2vwealth

                 Low  Mid High
  Less frequent  494 1605 3191
  Twice a day    843 3350 8588

bro2vQOL

                 Low  Mid High
  Less frequent 1947 2576  837
  Twice a day   4689 6363 1918

etc...

I already subset my database (dbmv) to keep only the variables I want and they are all factors.

I tried 2 options but both get errors:

  for(i in dbmv) {
  assign(paste("bro2v", i, sep="") ,table(dbmv$bro2,i))
  }

is working almost fine, but cycling through dbmv is in fact cycling through the values of my factors and not through the vectors themselves generating some strange tables.

Those names are understandable:

bro2v11
               i
                11 ans 13 ans 15 ans
  Less frequent   2018   1827   1630
  Twice a day     4276   4782   4194

bro2vBoy
               i
                 Boy Girl
  Less frequent 3342 2133
  Twice a day   6010 7242

But those are not easily guessed since many of my vectors have values named Low/Mid/High so I presume the last one overrides the other:

> bro2vLow
               i
                 Low  Mid High
  Less frequent 1947 2576  837
  Twice a day   4689 6363 1918

> bro2vMid
               i
                 Low  Mid High
  Less frequent  494 1605 3191
  Twice a day    843 3350 8588

And some tables are totally impossible to guess (bro2vNA is a good example)

I tried some changes like using nrow(dbmv) etc... but I can't seem to find the way to point to the factors names.

My other try was using apply:

apply(dbmv, margin=2, function(x) 
  {
  assign(paste("bro2v", x, sep="") ,table(dbmv$bro2,x))
  })

where I got a Error in match.fun(FUN) : argument "FUN" is missing, with no default message and I don't get where the error is (I presume some simple syntax mistake).

Could you tell me if those solutions are good for my problem and where I went wrong? Thx

EDIT

I tried to generate the tables using the provided function in which I added (after the print line):

assign(paste0("bro2v", var_name), table(data[[var]], data[[var_name]]))

I don't get any error message but nothing happens. This seems to work well outside of a function, so I assume it's the var_name in paste0 that needs something, or the assign function itself that does not react well in a loop.

Upvotes: 0

Views: 257

Answers (1)

Benjamin Schlegel
Benjamin Schlegel

Reputation: 527

I wrote you a function who can do that. The parameter var is the variable you want to get tables with all other variables. data is the data.frame.

get_tables = function(var, data){
  var_names = colnames(data)
  var_names = var_names[-which(var_names == var)]
  result = list()
  for(var_name in var_names){
    cat(paste0("bro2v", var_name))
    print(table(data[[var]], data[[var_name]]))
    result[[paste0("bro2v", var_name)]] =  table(data[[var]], data[[var_name]])
  }
  result
}
tables = get_tables("A", data)

Upvotes: 0

Related Questions