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"))
sapply(DF1[as.character(DF2[,1])], class)
assuming thatDF2
have the first column with all the column names (based on OP's descriptionetc 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