r - How can i rapidly explore the classes of all columns in a dataframe? -
i want rapidly explore classes of columns in dataframe, made function print columns names , arrange them class. want tell me numbers of columns aren't of class factor.
columnsclass<-function (x){ a<-vector() b<-vector(mode="character") c<-vector c=0 (i in 1:dim(x)[2]){ a[i]<-paste(class(x[,i]),names(x)[i],sep="--") if (class(x[,i])!= "factor"){ c<-c+1 b[c]<<-i }} #1st print print(sort(a)) #2nd print print(paste("columns aren't factors number:",paste(b,collapse=","),collapse=" ")) }
however when run it, doesn't #2nd print though code working.
> columnsclass(cars) [1] "numeric--dist" "numeric--speed" [1] "columns aren't factors number: " #it doesn't print numbers of columns of class factor if run separately ,it runs > print(paste("columns aren't factors number:",paste(b,collapse=","),collapse=" ")) [1] "columns aren't factors number: 1,2"
here's function takes account pierre's comments, simplifies a
's calculation well:
columnsclass <- function(x){ nm <- sapply(x, class) <- paste(nm, names(nm), sep = "--") b <- which(!sapply(x, is.factor)) # 1st print print(sort(a)) # 2nd print print(paste("columns aren't factors number:", paste(b, collapse = ","), collapse = " ")) } > columnsclass(cars) [1] "numeric--dist" "numeric--speed" [1] "columns aren't factors number: 1,2"
Comments
Post a Comment