Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
359 views
in Technique[技术] by (71.8m points)

r - How to change name of factor levels?

training set

trainSample <- cbind(data[1:980,1], data[1:980,2]) cl <-
factor(c(data[1:980,3]))

test set

testSample <- data(data[981:1485,1], data[981:1485,2])
cl.test <- clknn

prediction

k <- knn(trainSample, testSample, cl, k = 5)

output

< k

  [1] 2 2 1 1 1 1 2 1 2 1 1 2 2 2 2 2 1 1 2 2 2 2 2 2 2 2 2 2 2 1 2 2 1 1 2 2 1 1 2 2 2 2 1 2 2 2 2 2 2 1 2 2 2 2 2 2 2 2 2
 [60] 2 2 2 2 1 2 2 2 2 1 2 2 1 2 2 2 1 1 2 1 2 2 1 1 1 2 1 2 2 2 1 2 2 2 2 2 1 2 1 2 2 2 2 2 2 2 2 1 2 2 2 2 1 2 2 2 2 2 2
[119] 2 2 2 1 2 2 2 2 2 2 2 1 2 2 2 2 2 2 2 2 2 1 2 2 2 2 1 2 1 1 1 1 2 2 2 2 2 2 2 2 1 2 1 2 2 2 2 2 2 1 2 2 1 2 1 2 2 2 2
[178] 2 2 2 2 1 1 2 2 2 2 2 2 2 2 2 1 1 1 1 2 2 2 2 2 2 2 2 2 2 1 2 2 2 2 2 2 2 1 2 1 2 2 2 2 2 2 2 2 2 2 2 2 2 1 2 2 2 2 1
[237] 2 2 2 2 2 1 2 2 1 2 2 1 2 2 2 2 2 1 2 2 2 2 2 2 2 1 2 2 2 2 2 2 1 2 2 1 2 2 2 2 1 2 1 2 2 2 2 1 1 2 1 2 2 2 2 1 2 2 2
[296] 2 2 2 1 2 1 2 1 1 1 2 1 2 2 1 1 2 2 1 2 1 2 2 1 2 2 2 1 2 2 2 2 2 1 2 2 2 1 2 2 2 1 2 2 2 2 2 2 2 1 2 1 1 2 2 2 1 1 2
[355] 1 2 1 2 1 2 1 2 2 2 2 2 2 1 1 1 2 1 2 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 1 2 2 1 2 2 2 2 2 1 2 1 2 2 2 2 2 2 2 2 2 2 2 2 2
[414] 2 2 1 2 2 2 2 2 2 2 2 2 1 1 2 2 2 1 2 2 2 2 2 2 2 2 2 2 1 2 2 2 2 2 2 2 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
[473] 2 2 2 2 2 1 1 2 2 2 2 2 1 2 2 1 1 2 2 1 2 2 1 2 1 2 2 1 2 2 2 2 2
Levels: 1 2

I want "c" and "not-c" (like in my original data.csv), instead of 1 and 2 (im also not sure which number is supposed to represent which)

Can anyone help ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It is very easy to change the factor levels and also not get confused about which is which:

Example data:

> a <- factor(rep(c(1,2,1),50))
> a
  [1] 1 2 1 1 2 1 1 2 1 1 2 1 1 2 1 1 2 1 1 2 1 1 2 1 1 2 1 1 2 1 1 2 1 1 2 1 1 2 1 1 2 1 1 2 1 1 2 1 1 2 1 1 2 1 1 2 1 1 2 1 1 2 1 1 2 1 1 2 1 1 2 1 1 2
 [75] 1 1 2 1 1 2 1 1 2 1 1 2 1 1 2 1 1 2 1 1 2 1 1 2 1 1 2 1 1 2 1 1 2 1 1 2 1 1 2 1 1 2 1 1 2 1 1 2 1 1 2 1 1 2 1 1 2 1 1 2 1 1 2 1 1 2 1 1 2 1 1 2 1 1
[149] 2 1
Levels: 1 2

#this will help later as a verification
#this counts the instances for 1 and 2
> table(a)
a
  1   2 
100  50 

So as you can see above the order of the levels is 1 first and 2 second. When you change the levels (below) the order remains the same:

#the assignment function levels can be used to change the levels
#the order will remain the same i.e. 'c' for '1' and 'not-c' for '2'
levels(a) <- c('c', 'not-c')

> a
  [1] c     not-c c     c     not-c c     c     not-c c     c     not-c c     c     not-c c     c     not-c c     c     not-c c     c     not-c c    
 [25] c     not-c c     c     not-c c     c     not-c c     c     not-c c     c     not-c c     c     not-c c     c     not-c c     c     not-c c    
 [49] c     not-c c     c     not-c c     c     not-c c     c     not-c c     c     not-c c     c     not-c c     c     not-c c     c     not-c c    
 [73] c     not-c c     c     not-c c     c     not-c c     c     not-c c     c     not-c c     c     not-c c     c     not-c c     c     not-c c    
 [97] c     not-c c     c     not-c c     c     not-c c     c     not-c c     c     not-c c     c     not-c c     c     not-c c     c     not-c c    
[121] c     not-c c     c     not-c c     c     not-c c     c     not-c c     c     not-c c     c     not-c c     c     not-c c     c     not-c c    
[145] c     not-c c     c     not-c c    
Levels: c not-c

And this is the verification:

> table(a)
a
    c not-c 
  100    50 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...