r - What is the difference between following lines of code? -


here's code

levels(data[,7])           ## output levels of column in vector levels(data[,7])[data[,7]] ## not 100% sure 

i think second 1 gives vector of non-duplicate values (as far got). clarification appreciated.

the first line shows levels of factor variable in data[,7] - is, unique values of factor.

the second line uses values in data[,7] index unique levels. which, in case, gives data[,7].

it's useful construct if instead of levels have vector of colors want use different points in plot.

> levels(data[,2])[data[,2]]  [1] "a" "b" "b" "b" "c" "b" "a" "a" "b" "b" "c" "b" "a" "c" "a" "c" "a" "a" "a" "a" > c("red", "blue", "green")[data[,2]]  [1] "red"   "blue"  "blue"  "blue"  "green" "blue"  "red"   "red"   "blue"  "blue"  [11] "green" "blue"  "red"   "green" "red"   "green" "red"   "red"       "red"   "red"  

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -