0
votes

Confusing title, sorry, I am trying to print out a list of the classes of each variable in a DF that is contained in another DF.

DF1=50 variables of different types (Variable1, Variable2, etc.)

DF2=list of 15 variables (Variable1name, variable2name, variable3name, etc in one column)

How do I output the class of the variable in DF1 based on the name in DF2?

I thought this would do it:

class(DF1[,DF2[1,]])

Butit doesnt work.

DF2[1,] prints out the right name but then says it has 18 levels, listing all the other names in DF2...am I thinking about this wrong?

Goal is to look at all columns in DF1 if they are factor variables and get the levels of the factors into varmap. DF2 would have the datatypes of DF1

Edit: more code

vartype=matrix(0,nrow=nrow(DF2),ncol=1)
for (i in 1:nrow(DF2))
{
  temp=class(DF1[,DF2[i,]])
  if (temp=="Factor")
  { 
    DF2[i]=1;       
  }
}

varmap=list();
length(varmap)=nrow(DF2);
for (i in 1:nrow(DF2))
{
  if (DF2[i]=="factor")
  {
    varmap[[i]]=unique(DF1[,DF2[i]])
  }
}

Some data:

DF1 <- data.frame(
  X = sample(1:10),
  Y = sample(c("yes", "no"), 10, replace = TRUE)
)

str(Data)
DF2=as.data.frame(c("X","Y"))
1
Try sapply(DF1[as.character(DF2[,1])], class) assuming that DF2 have the first column with all the column names (based on OP's description etc in one column). But, from the code, it looks like it is from the first row. If that is the case, sapply(DF1[as.character(unlist(DF2[1,]))], class)akrun
A simple reproducible example would make it much easier to test possible solutions.MrFlick
Added some more code and dataAdam Sanders

1 Answers

0
votes

I think what you want is the following:

sapply(DF1[, DF2[,1]], class)

What this does is first subset DF1 to only include those columns which are named in DF2, then maps the "class" function to each column, sapply makes it return a vector. To get the class of each column in a dataset you need to us a mapping function like lapply, or a for loop. For instance lapply(mtcars, class gives you the class of each column.