Hello, My question sounds simple, but as I am desperatly searchin for a solution I am asking you all. :) I try to filter out all non-numeric columns of a data frame using a for loop to go through all columns. My if clause looks like this: for(j in 1:length(data)) { ... if(!is.numeric(data[j])) skip <- "not numeric") ... } When I do: is.numeric(data$vol) i get TRUE as result: unfortunately this is not applicable in a for loop, because i need to go through all of the columns using j. I also tried to paste data$ and the name of the column , but this is always a character. When I do: is.numeric(data[257]) i get FALSE as result (257 is the corresponding column index to "vol") Help needed! Thanks! Markus [[alternative HTML version deleted]]
Henrique Dallazuanna
2008-Jul-26 14:44 UTC
[R] Remove non-numerical columns from data frame
Try this: x <- data.frame(A=c(10,20), B = c("a", "b"), C = c(20,30)) x[sapply(x, is.numeric)] On Sat, Jul 26, 2008 at 11:19 AM, Markus M?hlbacher <muehliman at yahoo.com> wrote:> Hello, > > My question sounds simple, but as I am desperatly searchin for a solution I am > asking you all. :) > > I try to filter out all non-numeric columns of a data frame using a for loop > to go through all columns. My if clause looks like this: > > for(j in 1:length(data)) { > > ... > if(!is.numeric(data[j])) skip <- "not numeric") > ... > } > > When I do: is.numeric(data$vol) i get TRUE as result: > unfortunately this is not applicable in a for loop, because i need to go > through all of the columns using j. > I also tried to paste data$ and the name of the column , but this is always a > character. > > When I do: is.numeric(data[257]) i get FALSE as result > (257 is the corresponding column index to "vol") > > Help needed! Thanks! > Markus > > > > > > [[alternative HTML version deleted]] > > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > >-- Henrique Dallazuanna Curitiba-Paran?-Brasil 25? 25' 40" S 49? 16' 22" O