Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy does str() show incorrect info for factor levels after creating a sub-matrix in R?
    primarykey
    data
    text
    <p>I have the following data frame in R with 274569 rows and 15 columns:</p> <pre><code>&gt; str(x2) 'data.frame': 274569 obs. of 15 variables: $ ykod : int 99 99 99 99 99 99 99 99 99 99 ... $ yad : Factor w/ 43 levels "BAKUGAN","BARBIE",..: 2 2 2 2 2 2 2 2 2 2 ... $ per : Factor w/ 3 levels "2 AYLIK","3 AYLIK",..: 3 3 3 3 3 3 3 3 3 3 ... $ donem: int 201106 201106 201106 201106 201106 201106 201106 201106 201106 201106 ... $ sayi : int 201106 201106 201106 201106 201106 201106 201106 201106 201106 201106 ... $ mkod : int 359 361 362 363 366 847 849 850 1505 1506 ... $ mad : Factor w/ 11045 levels " Hilal Gida ",..: 5163 3833 10840 8284 10839 2633 10758 10293 6986 6984 ... $ mtip : Factor w/ 30 levels "Abone Bürosu ",..: 20 20 20 20 20 2 2 2 11 11 ... $ kanal: Factor w/ 2 levels "OB","SS": 2 2 2 2 2 2 2 2 1 1 ... $ bkod : int 110006 110006 110006 110006 110006 110006 110006 110006 110006 110006 ... $ bad : Factor w/ 213 levels "4. Levent","500 Evler",..: 25 25 25 25 25 25 25 25 25 25 ... $ bolge: Factor w/ 12 levels "Adana Şehiriçi",..: 7 7 7 7 7 7 7 7 7 7 ... $ sevk : int 5 2 2 2 10 0 4 3 13 32 ... $ iade : int 0 2 1 2 4 0 3 2 0 8 ... $ satis: int 5 0 1 0 6 0 1 1 13 24 ... </code></pre> <p>I create a sub-matrix and display its structure:</p> <pre><code>&gt; msub &lt;- x2[x2$ykod == 99,] &gt; str(msub) 'data.frame': 14367 obs. of 15 variables: $ ykod : int 99 99 99 99 99 99 99 99 99 99 ... $ yad : Factor w/ 43 levels "BAKUGAN","BARBIE",..: 2 2 2 2 2 2 2 2 2 2 ... $ per : Factor w/ 3 levels "2 AYLIK","3 AYLIK",..: 3 3 3 3 3 3 3 3 3 3 ... $ donem: int 201106 201106 201106 201106 201106 201106 201106 201106 201106 201106 ... $ sayi : int 201106 201106 201106 201106 201106 201106 201106 201106 201106 201106 ... $ mkod : int 359 361 362 363 366 847 849 850 1505 1506 ... $ mad : Factor w/ 11045 levels " Hilal Gida ",..: 5163 3833 10840 8284 10839 2633 10758 10293 6986 6984 ... $ mtip : Factor w/ 30 levels "Abone Bürosu ",..: 20 20 20 20 20 2 2 2 11 11 ... $ kanal: Factor w/ 2 levels "OB","SS": 2 2 2 2 2 2 2 2 1 1 ... $ bkod : int 110006 110006 110006 110006 110006 110006 110006 110006 110006 110006 ... $ bad : Factor w/ 213 levels "4. Levent","500 Evler",..: 25 25 25 25 25 25 25 25 25 25 ... $ bolge: Factor w/ 12 levels "Adana Şehiriçi",..: 7 7 7 7 7 7 7 7 7 7 ... $ sevk : int 5 2 2 2 10 0 4 3 13 32 ... $ iade : int 0 2 1 2 4 0 3 2 0 8 ... $ satis: int 5 0 1 0 6 0 1 1 13 24 ... </code></pre> <p>Now I have a sub-matrix with 14367 rows and 15 columns, but the levels of factors are still there. They should have been decreased. For example, for <code>yad</code>, there should be only one factor.</p> <p>How can I <em>easily</em> make str() to show correct info for factor levels so that when I type <code>str(msub)</code> it gives me correct values?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload