r - Whole dataset shows up, although a subset has been selected and newly defined -


i dataframe have subsetted using normal indexing. code below.

dframe <- dframe[1:10, c(-3,-7:-10)] 

but when write dframe$symbol output.

baba orcl lfc  tsm  act  abbv ma   abev kmi  ups  3285 levels: aa aa^b aac aan aap aat aav ab abb abbv abc abev abg abm abr abr^a abr^b abr^c abrn abt abx acc acco ace acg ach aci acm acn acp acre act act^a acw adc adm adpt ads adt adx aeb aec aed aee aeg aeh aek ael aem aeo aep aer aes aes^c aet af af^c ... zx 

i'm wondering happening here. dframe dataframe contain 10 rows or still rows, outputs 10 rows?

thanks

that's way factors work. when subset factor, preserves levels, no longer represented in subset. example:

f1 <- factor(letters); f1; ##  [1] b c d e f g h j k l m n o p q r s t u v w x y z ## levels: b c d e f g h j k l m n o p q r s t u v w x y z f2 <- f1[1:10]; f2; ##  [1] b c d e f g h j ## levels: b c d e f g h j k l m n o p q r s t u v w x y z 

to answer question, it's tricky append missing levels factor. have combine existing factor data missing indexes (here i'm referring integer indexes factor class internally uses map actual factor data levels vector, stored attribute on factor object), , rebuild factor (using original levels) combined data. below demonstrate this, randomizing subset taken f1 demonstrate order not matter:

set.seed(1); f3 <- sample(f1,10); f3; ##  [1] g j n u e s w m l b ## levels: b c d e f g h j k l m n o p q r s t u v w x y z factor(c(f3,setdiff(1:nlevels(f3),as.integer(f3))),labels=levels(f3)); ##  [1] g j n u e s w m l b c d f h k o p q r t v x y z ## levels: b c d e f g h j k l m n o p q r s t u v w x y z 

Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -