converting a DocumentTermMatrix to factor
0
0
Entering edit mode
7 weeks ago
hasani.iut6 ▴ 70

Hi everyone,

I have variable with type of DocumentTermMatrix, and I want to replace any element of it by zero and one(if element value > 0 is equal to 1 and if not is equal to 0)

I wrote the following function:

yes_or_no = function(x)
{
  y = ifelse(x>0 , 1,0)
  y = factor(y , levels = c(0,1) , labels = c("no","yes"))
  y
}
text_train = apply(text_freq_word_train, 2,yes_or_no)

the output doesn't seem to be correct.

head(inspect(text_freq_word_train)) = 

<<DocumentTermMatrix (documents: 7168, terms: 2114)>>
Non-/sparse entries: 64968/15088184
Sparsity           : 100%
Maximal term length: 13
Weighting          : term frequency (tf)
Sample             :
      Terms
Docs   job million obama percent presid say state tax vote year
  1281   0       3     0       2      1   1     7   2    2    3
  1619   0       0     0       0      0   0     0   0    0    0
  2143   3       0     2       0      3   0     0   0    0    1
  356    0       0     0       0      0   0     1   0    1    0
  3928   0       1     0       0      0   0     0   0    0    1
  3987   0       0     0       0      0   3     0   0    0    0
  428    4       0     0       0      0   0     0   0    0    0
  5157   0       0     0       0      0   0     0   1    0    1
  5532   0       0     0       0      0   1     1   2    1    0
  6120   1       0     2       0      4   2     0   0    0    0
      Terms
Docs   job million obama percent presid say state tax vote year
  1281   0       3     0       2      1   1     7   2    2    3
  1619   0       0     0       0      0   0     0   0    0    0
  2143   3       0     2       0      3   0     0   0    0    1
  356    0       0     0       0      0   0     1   0    1    0
  3928   0       1     0       0      0   0     0   0    0    1
  3987   0       0     0       0      0   3     0   0    0    0

text_train[1:10,1:10] = 
 Terms
Docs abort anni  demand group list  polit say   support third trimest
  1  "YES" "YES" "YES"  "YES" "YES" "YES" "YES" "YES"   "YES" "YES"  
  2  "NO"  "NO"  "NO"   "NO"  "NO"  "NO"  "NO"  "NO"    "NO"  "NO"   
  3  "NO"  "NO"  "NO"   "NO"  "NO"  "NO"  "NO"  "NO"    "NO"  "NO"   
  4  "NO"  "NO"  "NO"   "NO"  "NO"  "NO"  "NO"  "NO"    "NO"  "NO"   
  5  "NO"  "NO"  "NO"   "NO"  "NO"  "NO"  "NO"  "NO"    "NO"  "NO"   
  6  "NO"  "NO"  "NO"   "NO"  "NO"  "NO"  "NO"  "NO"    "NO"  "NO"   
  7  "NO"  "NO"  "NO"   "NO"  "NO"  "NO"  "NO"  "NO"    "NO"  "NO"   
  8  "NO"  "NO"  "NO"   "NO"  "NO"  "NO"  "NO"  "NO"    "NO"  "NO"   
  9  "NO"  "NO"  "NO"   "NO"  "NO"  "NO"  "NO"  "NO"    "NO"  "NO"   
  10 "NO"  "NO"  "NO"   "NO"  "NO"  "NO"  "YES" "NO"    "NO"  "NO" 

where did I go wrong?

DocumentTermMatrix offtopic convert factor • 183 views
ADD COMMENT
0
Entering edit mode

This post does not fit the theme of this forum.

ADD REPLY

Login before adding your answer.

Traffic: 1142 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6