Skip to content
Prev 279964 / 398513 Next

as.factor does not work inside function

Hi Josh,

Your suggesstion works, and the following also works:
as.factor.loop <- function(df, cols){

	if (!is.null(df) && !is.null(cols) && length(cols) > 0)
	{
		for(col in cols)
    {
			df[[col]] <- as.factor(df[[col]])
		}
	}
  df
}
[1] TRUE

Thanks.

Xiaobo Gu
On Sun, Dec 11, 2011 at 10:23 AM, Joshua Wiley <jwiley.psych at gmail.com> wrote: