[R] vectorization

Duncan Murdoch murdoch.duncan at gmail.com
Wed Jan 29 13:31:28 CET 2014


On 14-01-29 6:41 AM, Bill wrote:
> Hi. I saw this example and I cannot begin to figure out how it works. Can
> anyone give me an idea on this?
>
> n = 9e6
> df = data.frame(values = rnorm(n),
>                  ID = rep(LETTERS[1:3], each = n/3),
>                  stringsAsFactors = FALSE)
>> head(df)
>        values ID
> 1 -0.7355823  A
> 2 -0.4729925  A
> 3 -0.7417259  A
> 4  1.7633367  A
> 5 -0.3006790  A
> 6  0.6785947  A
>
>
> The idea is to replace all occurrences of A by    'Text for A'.
>
> He does this:
>
> translator_vector = c(A = 'Text for A',
>                        B = 'Text for B',
>                        C = 'Text for C')
>
> and subset this vector using df$ID:
>
> dum_vectorized = translator_vector[df$ID]
>
> It works but I have no idea why.

He is indexing by name.  The translator_vector looks like this:

            A            B            C
"Text for A" "Text for B" "Text for C"

The first element is named A, the second B, the third C.

So translator_vector["A"] is the same as translator_vector[1].  The ID 
column in your dataframe is a vector of strings to be used as names, so 
each one pulls out one element from the translator_vector.

Duncan Murdoch

>
> Thank you.
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>




More information about the R-help mailing list