I have created a loop which I am using to select all of the factor variables from a large data frame. However the loop does not work because the use of a counter to index the column for some reason doesn't read as a factor. Why is this? And how can I get this loop to work:
#Here's the loop:
y <- data.frame(c = 1:2661)
for (i in 1:ncol(x)){
ifelse(is.factor(x[i]) == FALSE, y <- cbind(y, x[i]), y <- y)
}
And the problem is clearly in the use of "i" to reference the column. For example:
#sample data
df <- as.data.frame(structure(c(2L, 2L, 2L, 2L, 2L),
.Label = c("female", "male"), class = "factor"))
names(df)[names(df) == "structure(c(2L, 2L, 2L, 2L, 2L), .Label = c(\"female\", \"male\"), class = \"factor\")"] <- "var"
#reference the column name directly
is.factor(df$var)
[1] TRUE
#use a counter to access the variable:
i <- 1
is.factor(df[i])
[1] FALSE
Is this something to do with R or is there something up with my PC? If it is something to do with R, can anyone explain what is going on and how to get my loop to function?