I have a large dataframe with 500+ columns with a variety of classes in a variety of orders. Example data:
col1 col2 col3 col4 col5 col6
2 red 1.2 5 5 5.7
4 banana 2.3 0 6 5.2
8 two 2.4 9 8 5.4
2 first 1.6 7 9 5.9
I am trying to change the class of the columns based on what their current class is. For example, I want too change every column that is currently class "integer" to class "numeric", but leave all the columns that are currently class "character" untouched. Is this possible?
I tried using a for loop, but when I set it up this way, it reads the column classes as "data.frame" instead of "numeric".
for(i in 1:ncol(df)){
ifelse(class(df[,i])=="integer",
as.numeric(df[,i]),NA)
}
I also tried to use apply but couldn't figure out how to code the condition correctly or have it loop through only some of my columns without creating a vector specifically naming all the columns with the class type I want changed.
cols <- colnames(df[,which(is.Class("integer"))])
df[,cols] = apply(df[,cols], 2, function(x) as.numeric(x));
Does anyone know if it's possible to do this and if so, how? Thank you!