r - What is the name of this syntax trick & where is it documented? -


haven't run before. page of pairs.panels in package psych, 1 finds following:

data(iris) pairs.panels(iris[1:4],bg=c("red","yellow","blue")[iris$species],pch=21) 

i want ask argument, sets background color of circles drawn data points: bg=c("red","yellow","blue")[iris$species] clearly, argument associates 3 levels of iris$species, factor, 3 colors given. i'm not asking does.

i wondering way of associating arguments passed data levels on fly called, , documented? seems r magic. if writing function, pass colors , column name of factor separately , make association manually behind scenes. trick useful. on face of [iris$species] looks data indexing itself. can't type [iris$species] in console instance, gives error. can type c("red","yellow","blue")[iris$species] , correct answer. seems there might recycling going on, i'm not sure. i'd curious documented, , if can explain what's happening in short sentence or two. instance, [iris$species] being converted integer, used index list of 3 colors? i'm thinking that's it, i'd opinion.

note: same trick used in graphics::pairs on panels.pairs based on.

there 2 things going on here:

  1. the factor iris$species being coerced numeric/integer.
  2. these integer indices being used in usual way.

coercion

this important because factor labels not red/yellow/blue in case:

> all( c("red","yellow","blue")[iris$species] == c("red","yellow","blue")[as.integer(iris$species)] ) [1] true > all( c("red","yellow","blue")[iris$species] == c("red","yellow","blue")[as.character(iris$species)] ) [1] na 

indexing repeated elements

in r, whenever index simple vector, elements of index repeated included repeatedly.

> x <- letters[1:5] > x [1] "a" "b" "c" "d" "e" > x[c(1,3)] [1] "a" "c" > x[c(1,3,3,3,3)] [1] "a" "c" "c" "c" "c" 

this commonly exploited when sampling replacement.

where documented?

in variety of places, although it's not emphasized how cool is.

for instance, on page 11, w. n. venables, d. m. smith, , r development core team. introduction r. notes on r: programming environment data analysis , graphics. version 2.5.0 (2007-04-23). states:

> x[1:10] selects first 10 elements of x (assuming length(x) not less 10). > c("x","y")[rep(c(1,2,2,1), times=4)] (an admittedly unlikely thing do) produces character vector of length 16 consisting of "x", "y", "y", "x" repeated 4 times. 

Comments

Popular posts from this blog

javascript - DIV "hiding" when changing dropdown value -

Does Firefox offer AppleScript support to get URL of windows? -

android - How to install packaged app on Firefox for mobile? -