FAQ
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 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 <- 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

Search Discussions

Discussion Posts

Previous

Follow ups

Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 2 of 4 | next ›
Discussion Overview
groupr-help @
categoriesr
postedDec 11, '11 at 2:06a
activeDec 11, '11 at 2:56a
posts4
users2
websiter-project.org
irc#r

2 users in discussion

Xiaobo Gu: 3 posts Joshua Wiley: 1 post

People

Translate

site design / logo © 2022 Grokbase