7

Say I have the following df

x<-c(1,3,2,4,1,3,2,4,1,3,2,4)
y<-c(rnorm(12))
df<-data.frame(x,y)

x is integer and I try to convert it to a factor using the suggestion from the R cookbook:

df[,'x']<-factor(df[,'x'])

But it still remains an integer. I have tried a solution found on the forum

df[,'x']<-lapply(df[,'x'], factor)

but it did not work either. Thanks.

M--
  • 25,431
  • 8
  • 61
  • 93
Vasile
  • 1,017
  • 2
  • 10
  • 19

5 Answers5

11

It seems that your original solution works. Check the typeof() as well as the class():

x<-c(1,3,2,4,1,3,2,4,1,3,2,4)
y<-c(rnorm(12))
df<-data.frame(x,y)
typeof(df$x)
#"double"    
class(df$x)
#"numeric"
df[,'x']<-factor(df[,'x'])
typeof(df$x)
 #"integer"
class(df$x)
#"factor"
erasmortg
  • 3,246
  • 1
  • 17
  • 34
  • 1
    Thanks, erasmortg. When I check with class it shows 'factor'. I hope it is indeed a factor then. Do not understand why typeof shows integer. I also just tried str(df) and it shows that x is a factor. – Vasile Jul 01 '15 at 15:18
4

may be try

df$x=as.factor(df$x)
Batanichek
  • 7,761
  • 31
  • 49
3

Try this.

df[,'x'] <- as.factor(as.character(df[,'x']))
Shiva
  • 789
  • 6
  • 15
1

The code

df[,'x']<-factor(df[,'x'])

does in fact work correctly. But its not so obvious using the x values you've chosen of 1,2,3,4. Try it with this instead:

x<-c(10,23,23, 12, 12, 12, 10, 10, 34, 45, 10, 23)
y<-c(rnorm(12))
df<-data.frame(x,y)
df[,'x']<-factor(df[,'x'])
str(df)

Now its easy to see that that the x column has Factor w/ 5 levels "10","12","23" ...

hackR
  • 1,459
  • 17
  • 26
0

this will work :

df[,'x'] <- as.factor(df[,'x'])
Liz Young
  • 408
  • 5
  • 17