Skip to content

as.factor does not work inside function

4 messages · Xiaobo Gu, Joshua Wiley

#
Hi,

I am trying to write a function do cast columns of data frame as
factor in a loop, the source is :


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]])
		}
	}
}


source('D:/ambertuil.r')
x <- 1:5
y <- 2:6
df <- data.frame(x=x, y=y)
as.factor.loop(df, c("x"))

But after the function call, the df data frame does not change,
because

is.factor(df[["x]])
FALSE

But if I call this in R console directlly, it works

for(col in c("x","y")){df[[col]] <- as.factor(df[[col]])}



is.factor(df[["x]])
FALSE


Regards,

Xiaobo Gu
#
I am sorry, it is

 for(col in c("x","y")){df[[col]] <- as.factor(df[[col]])}
 is.factor(df[["x]])
TRUE
On Sun, Dec 11, 2011 at 10:06 AM, Xiaobo Gu <guxiaobo1982 at gmail.com> wrote:
#
Hi Xiaobo,

The problem is that your function is not assigning the results to your
data frame---df is an internatl copy made by the function.  This is
done to prevent calling functions to have unexpected events such as
overwriting objects in the global environment.  Anyway, I think you
can accomplish what you want using lapply():

## your data
df <- data.frame(x=1:5, y=2:6)
## apply the function, as.factor() to all the elements in the first argument
## and save the results in the relevant columns of df
df["x"] <- lapply(df["x"], as.factor)

## check results
is.factor(df[, "x"])

Hope this helps,

Josh
On Sat, Dec 10, 2011 at 6:06 PM, Xiaobo Gu <guxiaobo1982 at gmail.com> wrote:

  
    
#
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: