[R] as.factor does not work inside function

2011-12-10 Thread Xiaobo Gu
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]] -

Re: [R] as.factor does not work inside function

2011-12-10 Thread 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 guxiaobo1...@gmail.com wrote: 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

Re: [R] as.factor does not work inside function

2011-12-10 Thread Joshua Wiley
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

Re: [R] as.factor does not work inside function

2011-12-10 Thread Xiaobo Gu
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]]) } }