bziggy
bziggy

Reputation: 461

R creating variable tables from list of variable names

I am currently trying to create a table from a list of variable names (something I feel should be relatively simple) and I can't for the life of me, figure out how to do it correctly.

I have a data table that I've named 'file' and there are a list of 3 variable names within this file. What I want to do is create a table of each variable and then rbind them together. For further context, these few lines of code will be worked into a much larger function. The list of variable names must be able to accommodate the number of variables the user defines.

I have tried the following:

file<-as.data.table(dt)
variable_list<-list("outcome", "type")

for (variable in variable_list){
var_table<-as.data.table(table(file$variable_list))
na_table<-as.data.table(table(is.na(file$variable)))
}

When I run the above code, R returns empty tables of var_table and na_table. What am I doing wrong?

Upvotes: 2

Views: 2054

Answers (2)

IRTFM
IRTFM

Reputation: 263481

The problem (at least one of the problems) might be that you are attempting to use the $ operator incorrectly. You cannot substitute text values into the second argument. You can use its syntactic equivalent [[ instead of $, however. So this would be a possible improvement. (I've not tested it since you provided no test material.)

file<-as.data.table(dt)
variable_list<-list("outcome", "type")

for (variable in variable_list){
var_table<-as.data.table(table(file[[variable]]))  # clearly  not  variable_list
na_table<-as.data.table(table(is.na(file[[variable]] )))
}

I'm guessing you might have done something like, ...

 var_table <- file[, table(variable ) ]

... since data.table syntax evaluates text values in the environment of the file (which in this case is confusing named "file". It's better not to use such names, since in this case there's also an R function by that name.

Upvotes: 1

akrun
akrun

Reputation: 887901

An option is to loop over the 'variable_list, extract the column, apply tableandrbindwithindo.call`

do.call(rbind, lapply(variable_list, function(nm) table(file[[nm]])))

NOTE: assuming that the levels of the columns are the same

If the levels are not the same, make it same by converting the columns to factor with levels specified

lvls <- na.omit(sort(unique(unlist(file[, unlist(variable_list), with = FALSE]))))

do.call(rbind, lapply(variable_list, function(nm)
       table(factor(file[[nm]], levels = lvls))))

Or if we have a data.table, use the data.table methods

rbindlist(lapply(variable_list, function(nm) file[, .N,by = c(nm)]), fill = TRUE)

Upvotes: 1

Related Questions