## Construct an unique index from two integer (Pairing Function)

April 4, 2013 1 comment

Recently, I need to construct an unique index from two integer. The best solution I found is the Pairing function.

Pairing function is an one to one and onto function that map two integers to a single integer. The definition as follows:

```pair<-function(x,y){
0.5*(x+y)*(x+y+1) +  x
}
unpair<-function(z){
w= floor( (sqrt(8*z+1) - 1)/2 )
t = w*(w+1)/2
cbind(z-t,w-z+t)
}

foreach (i = 0:4,.combine=rbind) %do% {
x<-0:i
y<-i:0

key<-pair(x,y)
unpair_key <- unpair(key)
cbind(x,y,key=key,unpair_key=unpair_key)
}
x y key x y
[1,] 0 0   0 0 0
[2,] 0 1   1 0 1
[3,] 1 0   2 1 0
[4,] 0 2   3 0 2
[5,] 1 1   4 1 1
[6,] 2 0   5 2 0
[7,] 0 3   6 0 3
[8,] 1 2   7 1 2
[9,] 2 1   8 2 1
[10,] 3 0   9 3 0
[11,] 0 4  10 0 4
[12,] 1 3  11 1 3
[13,] 2 2  12 2 2
[14,] 3 1  13 3 1
[15,] 4 0  14 4 0
```

If ordering of `x` and `y` is not important, we can swap `x` and `y` if `x>y`. However, the Pairing function is not one to one and we can not back out `x` and `y` with `z`

```pair<-cmpfun(function(x,y,ordering_matter=TRUE){
if (ordering_matter){
return(0.5*(x+y)*(x+y+1) + x)
} else{
swap <- x>y
return(0.5*(x+y)*(x+y+1) +  (x* !swap) + (y*swap ))
}
})

foreach (i = 0:4,.combine=rbind) %do% {
x<-0:i
y<-i:0

key<-pair(x,y,ordering_matter=FALSE)
unpair_key <- unpair(key)
cbind(x,y,key=key,unpair_key=unpair_key)
}
x y key x y
[1,] 0 0   0 0 0
[2,] 0 1   1 0 1
[3,] 1 0   1 0 1
[4,] 0 2   3 0 2
[5,] 1 1   4 1 1
[6,] 2 0   3 0 2
[7,] 0 3   6 0 3
[8,] 1 2   7 1 2
[9,] 2 1   7 1 2
[10,] 3 0   6 0 3
[11,] 0 4  10 0 4
[12,] 1 3  11 1 3
[13,] 2 2  12 2 2
[14,] 3 1  11 1 3
[15,] 4 0  10 0 4
>
```

If we have more than two integers, we can apply the Pairing function in a nested manner.

```nestedPair<-function(x){
ncol_x = ncol(x)
if(ncol_x==1){
return(x)
} else if(ncol_x ==2) {
return(pair(x[,1],x[,2]))
} else if ( ncol_x > 2){
return(pair( x[,1] ,nestedPair(x[,2:ncol_x]) ) )
}
}

nestedUnpair<-function(x,order){
if(order==1){
return(unpair(x))
} else if(order >1) {
out <- unpair(x)

return(cbind(out[,1],nestedUnpair(out[,2],order-1)))
}
}

x<-expand.grid(0:2,0:2,0:2)
key <- nestedPair(x)
unpair_key <- nestedUnpair(key,2)
cbind(x=x,key=key,unpair_key=unpair_key)

x.Var1 x.Var2 x.Var3 key unpair_key.1 unpair_key.2 unpair_key.3
1       0      0      0   0            0            0            0
2       1      0      0   2            1            0            0
3       2      0      0   5            2            0            0
4       0      1      0   3            0            1            0
5       1      1      0   7            1            1            0
6       2      1      0  12            2            1            0
7       0      2      0  15            0            2            0
8       1      2      0  22            1            2            0
9       2      2      0  30            2            2            0
10      0      0      1   1            0            0            1
11      1      0      1   4            1            0            1
12      2      0      1   8            2            0            1
13      0      1      1  10            0            1            1
14      1      1      1  16            1            1            1
15      2      1      1  23            2            1            1
16      0      2      1  36            0            2            1
17      1      2      1  46            1            2            1
18      2      2      1  57            2            2            1
19      0      0      2   6            0            0            2
20      1      0      2  11            1            0            2
21      2      0      2  17            2            0            2
22      0      1      2  28            0            1            2
23      1      1      2  37            1            1            2
24      2      1      2  47            2            1            2
25      0      2      2  78            0            2            2
26      1      2      2  92            1            2            2
27      2      2      2 107            2            2            2
```
Categories: Uncategorized

## A handy concatenation operator

It may be useful for you to define a concatenation operator for characters. Sometimes, I find this is more intuitive and handy than using `paste0 ` or `paste`. Also, it makes your code look better when you have nested paste, e.g.`paste0("Y~",paste0("z",1:3, "*x",1:3,collapse="+")`. The drawback is that it may reduce the readability of your code to other R user, since it is a self define function.(i guess it should be fine, cuz it is really intuitive. Also other scripting language also has similar concatenation operator)

```"%+%" <- function(...){
paste0(...,sep="")
}
> "hello" %+% "world"
[1] "helloworld"
"hello" %+% "world" %+% 1:3
[1] "helloworld1" "helloworld2" "helloworld3"
```

Generating formula:

```"Y~" %+% paste0("z",1:3, "*x",1:3,collapse="+")
[1] "Y~z1*x1+z2*x2+z3*x3"
```
Categories: Custom Function

## Compute the self excluded sample mean by group

egen(stata cmd) compute a summary statistics by groups and store it in to a new variable. For example, the data has three variables, id, time and y, we want to compute the mean of y by for each id and then store it as a new variable mean_y.

In stata, the command would be

egen mean_y = mean(y), by(id)

In R, this task can be completed by `ave`

Generate dataset:

```id <- rep(1:3,each=3)
t<-rep(1:3,3)
y<-sample(1:5,9,replace=T)
my_data<-data.frame(id=id,time=t,y=y)
```

Orignal data:

```> my_data
id time y
1  1    1 4
2  1    2 1
3  1    3 4
4  2    1 2
5  2    2 3
6  2    3 3
7  3    1 4
8  3    2 4
9  3    3 3
```
```> within(my_data, {mean_y = ave(y,id)} )
id time y   mean_y
1  1    1 4 3.000000
2  1    2 1 3.000000
3  1    3 4 3.000000
4  2    1 2 2.666667
5  2    2 3 2.666667
6  2    3 3 2.666667
7  3    1 4 3.666667
8  3    2 4 3.666667
9  3    3 3 3.666667
```

The default summary statistics is `mean`. However, we can assign a particular function to compute the summary statistics. For example, if we want to compute the sd of y by id, then we can have

```within(my_data, {sd_y = ave(y,id,FUN=sd)} )
id time y      sd_y
1  1    1 4 1.7320508
2  1    2 1 1.7320508
3  1    3 4 1.7320508
4  2    1 2 0.5773503
5  2    2 3 0.5773503
6  2    3 3 0.5773503
7  3    1 4 0.5773503
8  3    2 4 0.5773503
9  3    3 3 0.5773503
```

Remark: The `within` evaluate an expression in an environment created from the data.frame. In addition, it will modify the data.frame and return it back(in our case, it create new variables, mean_y or sd_y )

Here is another usage of `ave`. We would like to create a self excluded sample mean by group.

Suppose the data has three variables, id, time and y, we want to compute the mean of y by for each id but excluding the value of y of current time period.

```id <- rep(1:3,each=3)
t<-rep(1:3,3)
y<-sample(1:5,9,replace=T)
my_data<-data.frame(id=id,time=t,y=y)
```

Orignal data:

```> my_data
id time y
1  1    1 4
2  1    2 1
3  1    3 4
4  2    1 2
5  2    2 3
6  2    3 3
7  3    1 4
8  3    2 4
9  3    3 3
```

First, we need a function to compute the self excluded mean. This function takes a vector and a function(default is mean) as argument. It apply the function to the vector where one of the element is removed. The return value is a vector that i-th element is given by FUN(x[-i])

```excludeSelfSummary<-function(x,FUN=mean){
sapply(1:length(x), function(i) FUN(x[-i]))
}
> excludeSelfSummary(1:5,mean)
[1] 3.50 3.25 3.00 2.75 2.50
> excludeSelfSummary(1:5,min)
[1] 2 1 1 1 1
> excludeSelfSummary(1:5,max)
[1] 5 5 5 5 4
```

Then we pass the `excludeSelfSummary into ave as argument. `

``` > within(my_data, {sd_y = ave(y,id,FUN=excludeSelfSummary)} ) id time y sd_y 1 1 1 4 2.5 2 1 2 1 4.0 3 1 3 4 2.5 4 2 1 2 3.0 5 2 2 3 2.5 6 2 3 3 2.5 7 3 1 4 3.5 8 3 2 4 3.5 9 3 3 3 4.0 Of course, we could compute the self excluded minimum or maximum. > within(my_data, {sd_y = ave(y,id,FUN=function(x) excludeSelfSummary(x,min) )}) id time y sd_y 1 1 1 4 1 2 1 2 1 4 3 1 3 4 1 4 2 1 2 3 5 2 2 3 2 6 2 3 3 2 7 3 1 4 3 8 3 2 4 3 9 3 3 3 4 ```
``` Categories: data cleaning ```
``` How to do egen (stata cmd) in R February 12, 2013 TszKin Julian 2 comments egen(stata cmd) compute a summary statistics by groups and store it in to a new variable. For example, the data has three variables, id, time and y, we want to compute the mean of y by for each id and then store it as a new variable mean_y. In stata, the command would be egen mean_y = mean(y), by(id) In R, this task can be completed by ave Generate dataset: id <- rep(1:3,each=3) t<-rep(1:3,3) y<-sample(1:5,9,replace=T) my_data<-data.frame(id=id,time=t,y=y) Orignal data: > my_data id time y 1 1 1 4 2 1 2 1 3 1 3 4 4 2 1 2 5 2 2 3 6 2 3 3 7 3 1 4 8 3 2 4 9 3 3 3 > within(my_data, {mean_y = ave(y,id)} ) id time y mean_y 1 1 1 4 3.000000 2 1 2 1 3.000000 3 1 3 4 3.000000 4 2 1 2 2.666667 5 2 2 3 2.666667 6 2 3 3 2.666667 7 3 1 4 3.666667 8 3 2 4 3.666667 9 3 3 3 3.666667 The default summary statistics is mean. However, we can assign a particular function to compute the summary statistics. For example, if we want to compute the sd of y by id, then we can have within(my_data, {sd_y = ave(y,id,FUN=sd)} ) id time y sd_y 1 1 1 4 1.7320508 2 1 2 1 1.7320508 3 1 3 4 1.7320508 4 2 1 2 0.5773503 5 2 2 3 0.5773503 6 2 3 3 0.5773503 7 3 1 4 0.5773503 8 3 2 4 0.5773503 9 3 3 3 0.5773503 Remark: The within evaluate an expression in an environment created from the data.frame. In addition, it will modify the data.frame and return it back(in our case, it create new variables, mean_y or sd_y ) Categories: data cleaning, stata Generating a lag/lead variables March 11, 2012 TszKin Julian 10 comments A few days ago, my friend asked me is there any function in R to generate lag/lead variables in a data.frame or did similar thing as _n in stata. He would like to use that to clean-up his dataset in R. In stata help manual: _n contains the number of the current observation. Here’s an example to illustrate what _n does: set obs 10 generate x = _n generate x_lag1 = x[_n-1] generate x_lead1 = x[_n+1] The data generated would be : x = {1,2,3,4,5,6,7,8,9,10} x_lag1 = {NA,1,2,3,4,5,6,7,8,9} x_lead1 = {1,2,3,4,5,6,7,8,9,NA} The key feature is the new vector has the same length as the original vector, so we can use it with the original vector or other generated vector. One application is to create a MA series (just an example, it is better to use function in any time-series packages to do that) generate x_ma_1 = (x[_n-1] + x[_n]) / 2 I googled a while for that, basically there’re two types of method to generate lag/lead variables in R:(reference) 1> Function that generate a shorter vector (e.g. embed(), running() in gtools 2> Function in ts, zoo, xts, dynlm,dlm. However, both solutions do not solve his problem. Then I wrote a “shift” function to do the task: shift<-function(x,shift_by){ stopifnot(is.numeric(shift_by)) stopifnot(is.numeric(x)) if (length(shift_by)>1) return(sapply(shift_by,shift, x=x)) out<-NULL abs_shift_by=abs(shift_by) if (shift_by > 0 ) out<-c(tail(x,-abs_shift_by),rep(NA,abs_shift_by)) else if (shift_by < 0 ) out<-c(rep(NA,abs_shift_by), head(x,-abs_shift_by)) else out<-x out } # Example d<-data.frame(x=1:15) #generate lead variable d\$df_lead2<-shift(d\$x,2) #generate lag variable d\$df_lag2<-shift(d\$x,-2) > d x df_lead2 df_lag2 1 1 3 NA 2 2 4 NA 3 3 5 1 4 4 6 2 5 5 7 3 6 6 8 4 7 7 9 5 8 8 10 6 9 9 NA 7 10 10 NA 8 # shift_by is vectorized d\$df_lead2 shift(d\$x,-2:2) [,1] [,2] [,3] [,4] [,5] [1,] NA NA 1 2 3 [2,] NA 1 2 3 4 [3,] 1 2 3 4 5 [4,] 2 3 4 5 6 [5,] 3 4 5 6 7 [6,] 4 5 6 7 8 [7,] 5 6 7 8 9 [8,] 6 7 8 9 10 [9,] 7 8 9 10 NA [10,] 8 9 10 NA NA # Test library(testthat) expect_that(shift(1:10,2),is_identical_to(c(3:10,NA,NA))) expect_that(shift(1:10,-2), is_identical_to(c(NA,NA,1:8))) expect_that(shift(1:10,0), is_identical_to(1:10)) expect_that(shift(1:10,0), is_identical_to(1:10)) expect_that(shift(1:10,1:2), is_identical_to(cbind(c(2:10,NA),c(3:10,NA,NA)))) Notice that the result depends on how the data.frame is sorted. Categories: Custom Function Overhead cost of a function call October 2, 2011 TszKin Julian 3 comments Recently, I would like to apply unit testing method to my R program. The first thing i need to chop every few lines of the code into functions so that I can test each of them. A Question comes up to my mind: What is the overhead cost of a function call? To answer this question, i wrote the following : library(rbenchmark) library(compiler) f<-function(x,y){ x+y } g<-function(x,y){ f(x,y) } cmpf<-cmpfun(f) cmpg<-cmpfun(g) benchmark(1+2,f(1,2),g(1,2),cmpf(1,2),cmpg(1,2),cmpg2(1,2), replications =1000000, columns = c("test", "replications", "elapsed", "relative"),order='relative') test replications elapsed relative 1       1 + 2      1000000    4.00    1.000 4  cmpf(1, 2)      1000000    4.34    1.085 2     f(1, 2)      1000000    4.82    1.205 5  cmpg(1, 2)      1000000    5.44    1.360 3     g(1, 2)      1000000    5.68    1.420 The result suggests several things The overhead cost is about 0.82 second for 1,000,000 times function call. If we compile the function, the overhead cost is about 0.34 second for 1,000,000 times function call. I don’t know whether it is a huge cost, but I believe the benefit of cleaner writing code with unit testing must worth more than that! Categories: R programming Call by reference in R September 11, 2011 TszKin Julian 6 comments Sometimes it is convenient to use “call by reference evaluation” inside an R function. For example, if you want to have multiple return value for your function, then either you return a list of return value and split them afterward or you can return the value via the argument. For some reasons(I would like to know too), R do not support call by reference. The first reason come up in my mind is safety, if the function can do call by reference, it is more difficult to trace the code and debug(you have to find out which function change the value of your variables by examining the details of your function). In fact, R do “call by reference” when the value of the argument is not changed. They will make a copy of the argument only when the value is changed.  So we can expect there’s no efficiency gain (at least not a significant one) even we can do call by reference. Anyway, it is always good to know how to have a “pseudo call by reference” in R (you can choose (not) to use it for whatever reason). The trick to implement call by reference is to make use of the eval.parent function in R. You can add a code to replace the argument value in the parent environment so that the function looks like implementing the call by reference evaluation strategy. Here are some examples of how to do it: set<-function(x,value){ eval.parent(substitute(x<-value)) } valX <- 51 set(valX ,10) valX >[1] 10 addOne_1<-function(x,value){ eval.parent(substitute(x<-x+1)) } valX <- 51 addOne_1(valX) valX >[1] 52 Note that you could not change the value of x inside the function. If you change the value of x, a new object will be created. The substitute function will replace x with the new value and hence this method wont work. For example addOne_2<-function(x,value){ x<-x+1 eval.parent(substitute(x<-x)) } valX <- 51 addOne_2(valX) >Error in 52 <- 52 : invalid (do_set) left-hand side to assignment If you want to change the value of x inside the function, you have to copy x to a new object and use new object as x.  At the end of the function, you can replace the value of x with the new object at the parent environment. addOne_3<-function(x,value){ xx<-x xx<-xx+1 eval.parent(substitute(x<-xx)) } valX <- 51 addOne_3(valX) valX >[1] 52 Another way to do call by reference more formally is using the R.oo packages. Another way to implement Categories: R programming Older Entries ```
``` RSS feed Google Youdao Xian Guo Zhua Xia My Yahoo! newsgator Bloglines iNezha RSS - Posts Blog Stats 28,463 hits Recent Posts Construct an unique index from two integer (Pairing Function) A handy concatenation operator Compute the self excluded sample mean by group How to do egen (stata cmd) in R Generating a lag/lead variables Categories Custom Function data cleaning R programming stata Uncategorized Archives April 2013 February 2013 March 2012 October 2011 September 2011 Custom Function data cleaning R programming stata Uncategorized Search for: Blogroll My website Econometrics Econometric Sense R R-Bloggers Statistics Blog Statistics Statistics Blog __ATA.cmd.push(function() { __ATA.initDynamicSlot({ id: 'atatags-286348-5e58fa6c1c403', location: 140, formFactor: '003', label: { text: 'Advertisements', }, creative: { reportAd: { text: 'Report this ad', }, privacySettings: { text: 'Privacy settings', } } }); }); ```
``` Top Create a free website or blog at WordPress.com. ```
``` ```
``` //<![CDATA[ var infiniteScroll = JSON.parse( decodeURIComponent( '%7B%22settings%22%3A%7B%22id%22%3A%22main%22%2C%22ajaxurl%22%3A%22https%3A%5C%2F%5C%2Fctszkin.com%5C%2F%3Finfinity%3Dscrolling%22%2C%22type%22%3A%22scroll%22%2C%22wrapper%22%3Atrue%2C%22wrapper_class%22%3A%22infinite-wrap%22%2C%22footer%22%3Atrue%2C%22click_handle%22%3A%221%22%2C%22text%22%3A%22Older%20posts%22%2C%22totop%22%3A%22Scroll%20back%20to%20top%22%2C%22currentday%22%3A%2211.09.11%22%2C%22order%22%3A%22DESC%22%2C%22scripts%22%3A%5B%5D%2C%22styles%22%3A%5B%5D%2C%22google_analytics%22%3Afalse%2C%22offset%22%3A0%2C%22history%22%3A%7B%22host%22%3A%22ctszkin.com%22%2C%22path%22%3A%22%5C%2Fpage%5C%2F%25d%5C%2F%22%2C%22use_trailing_slashes%22%3Atrue%2C%22parameters%22%3A%22%22%7D%2C%22query_args%22%3A%7B%22error%22%3A%22%22%2C%22m%22%3A%22%22%2C%22p%22%3A0%2C%22post_parent%22%3A%22%22%2C%22subpost%22%3A%22%22%2C%22subpost_id%22%3A%22%22%2C%22attachment%22%3A%22%22%2C%22attachment_id%22%3A0%2C%22name%22%3A%22%22%2C%22pagename%22%3A%22%22%2C%22page_id%22%3A0%2C%22second%22%3A%22%22%2C%22minute%22%3A%22%22%2C%22hour%22%3A%22%22%2C%22day%22%3A0%2C%22monthnum%22%3A0%2C%22year%22%3A0%2C%22w%22%3A0%2C%22category_name%22%3A%22%22%2C%22tag%22%3A%22%22%2C%22cat%22%3A%22%22%2C%22tag_id%22%3A%22%22%2C%22author%22%3A%22%22%2C%22author_name%22%3A%22%22%2C%22feed%22%3A%22%22%2C%22tb%22%3A%22%22%2C%22paged%22%3A0%2C%22meta_key%22%3A%22%22%2C%22meta_value%22%3A%22%22%2C%22preview%22%3A%22%22%2C%22s%22%3A%22%22%2C%22sentence%22%3A%22%22%2C%22title%22%3A%22%22%2C%22fields%22%3A%22%22%2C%22menu_order%22%3A%22%22%2C%22embed%22%3A%22%22%2C%22category__in%22%3A%5B%5D%2C%22category__not_in%22%3A%5B%5D%2C%22category__and%22%3A%5B%5D%2C%22post__in%22%3A%5B%5D%2C%22post__not_in%22%3A%5B%5D%2C%22post_name__in%22%3A%5B%5D%2C%22tag__in%22%3A%5B%5D%2C%22tag__not_in%22%3A%5B%5D%2C%22tag__and%22%3A%5B%5D%2C%22tag_slug__in%22%3A%5B%5D%2C%22tag_slug__and%22%3A%5B%5D%2C%22post_parent__in%22%3A%5B%5D%2C%22post_parent__not_in%22%3A%5B%5D%2C%22author__in%22%3A%5B%5D%2C%22author__not_in%22%3A%5B%5D%2C%22posts_per_page%22%3A7%2C%22ignore_sticky_posts%22%3Afalse%2C%22suppress_filters%22%3Afalse%2C%22cache_results%22%3Afalse%2C%22update_post_term_cache%22%3Atrue%2C%22lazy_load_term_meta%22%3Atrue%2C%22update_post_meta_cache%22%3Atrue%2C%22post_type%22%3A%22%22%2C%22nopaging%22%3Afalse%2C%22comments_per_page%22%3A%2250%22%2C%22no_found_rows%22%3Afalse%2C%22order%22%3A%22DESC%22%7D%2C%22last_post_date%22%3A%222011-09-11%2021%3A27%3A26%22%2C%22stats%22%3A%22blog%3D25621776%26v%3Dwpcom%26tz%3D-5%26user_id%3D0%26subd%3Drlearner%26x_pagetype%3Dinfinite%22%7D%7D' ) ); //]]> var WPGroHo = {"my_hash":""}; //initialize and attach hovercards to all gravatars jQuery( document ).ready( function( \$ ) { if (typeof Gravatar === "undefined"){ return; } if ( typeof Gravatar.init !== "function" ) { return; } Gravatar.profile_cb = function( hash, id ) { WPGroHo.syncProfileData( hash, id ); }; Gravatar.my_hash = WPGroHo.my_hash; Gravatar.init( 'body', '#wp-admin-bar-my-account' ); }); R HEAD Create a free website or blog at WordPress.com. ( function () { var setupPrivacy = function() { var \$ = window.jQuery; if ( ! \$ ) return; // jQuery required \$( document ).ready( function() { // Minimal Mozilla Cookie library // https://developer.mozilla.org/en-US/docs/Web/API/Document/cookie/Simple_document.cookie_framework var cookieLib = {getItem:function(e){return e&&decodeURIComponent(document.cookie.replace(new RegExp("(?:(?:^|.*;)\\s*"+encodeURIComponent(e).replace(/[\-\.\+\*]/g,"\\\$&")+"\\s*\\=\\s*([^;]*).*\$)|^.*\$"),"\$1"))||null},setItem:function(e,o,n,t,r,i){if(!e||/^(?:expires|max\-age|path|domain|secure)\$/i.test(e))return!1;var c="";if(n)switch(n.constructor){case Number:c=n===1/0?"; expires=Fri, 31 Dec 9999 23:59:59 GMT":"; max-age="+n;break;case String:c="; expires="+n;break;case Date:c="; expires="+n.toUTCString()}return"rootDomain"!==r&&".rootDomain"!==r||(r=(".rootDomain"===r?".":"")+document.location.hostname.split(".").slice(-2).join(".")),document.cookie=encodeURIComponent(e)+"="+encodeURIComponent(o)+c+(r?"; domain="+r:"")+(t?"; path="+t:"")+(i?"; secure":""),!0}}; var setDefaultOptInCookie = function() { var value = '1YNN'; cookieLib.setItem( 'usprivacy', value, 365 * 24 * 60 * 60, '/', '.rootDomain' ); }; var setCcpaAppliesCookie = function( value ) { cookieLib.setItem( 'ccpa_applies', value, 24 * 60 * 60, '/', '.rootDomain' ); } var maybeCallDoNotSellCallback = function() { if ( 'function' === typeof window.doNotSellCallback ) { return window.doNotSellCallback( \$ ); } return false; } var usprivacyCookie = cookieLib.getItem( 'usprivacy' ); if ( null !== usprivacyCookie ) { maybeCallDoNotSellCallback(); return; } var ccpaCookie = cookieLib.getItem( 'ccpa_applies' ); if ( null === ccpaCookie ) { \$.ajax({ type: 'GET', dataType: "json", cache: false, url: 'https://public-api.wordpress.com/geo/', success: function( data ) { var ccpa_applies = data['region'] && data['region'].toLowerCase() === 'california'; setCcpaAppliesCookie( ccpa_applies ); if ( ccpa_applies ) { if ( maybeCallDoNotSellCallback() ) { setDefaultOptInCookie(); } } }, error: function() { setCcpaAppliesCookie( true ); if ( maybeCallDoNotSellCallback() ) { setDefaultOptInCookie(); } }, } ); } else { if ( ccpaCookie === 'true' ) { if ( maybeCallDoNotSellCallback() ) { setDefaultOptInCookie(); } } } } ); }; if ( window.defQueue && defQueue.isLOHP && defQueue.isLOHP === 2020 ) { defQueue.items.push( setupPrivacy ); } else { setupPrivacy(); } } )(); ( function( \$ ) { \$( document.body ).on( 'post-load', function () { if ( typeof __ATA.insertInlineAds === 'function' ) { __ATA.insertInlineAds(); } } ); } )( jQuery ); Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their use. To find out more, including how to control cookies, see here: Cookie Policy Post to Cancel (function(){ var corecss = document.createElement('link'); var themecss = document.createElement('link'); var corecssurl = "https://s1.wp.com/wp-content/plugins/syntaxhighlighter/syntaxhighlighter3/styles/shCore.css?ver=3.0.9b"; if ( corecss.setAttribute ) { corecss.setAttribute( "rel", "stylesheet" ); corecss.setAttribute( "type", "text/css" ); corecss.setAttribute( "href", corecssurl ); } else { corecss.rel = "stylesheet"; corecss.href = corecssurl; } document.head.appendChild( corecss ); var themecssurl = "https://s2.wp.com/wp-content/plugins/syntaxhighlighter/syntaxhighlighter3/styles/shThemeDefault.css?m=1363304414h&amp;ver=3.0.9b"; if ( themecss.setAttribute ) { themecss.setAttribute( "rel", "stylesheet" ); themecss.setAttribute( "type", "text/css" ); themecss.setAttribute( "href", themecssurl ); } else { themecss.rel = "stylesheet"; themecss.href = themecssurl; } document.head.appendChild( themecss ); })(); SyntaxHighlighter.config.strings.expandSource = '+ expand source'; SyntaxHighlighter.config.strings.help = '?'; SyntaxHighlighter.config.strings.alert = 'SyntaxHighlighter\n\n'; SyntaxHighlighter.config.strings.noBrush = 'Can\'t find brush for: '; SyntaxHighlighter.config.strings.brushNotHtmlScript = 'Brush wasn\'t configured for html-script option: '; SyntaxHighlighter.defaults['pad-line-numbers'] = false; SyntaxHighlighter.defaults['toolbar'] = false; SyntaxHighlighter.all(); // Infinite scroll support if ( typeof( jQuery ) !== 'undefined' ) { jQuery( function( \$ ) { \$( document.body ).on( 'post-load', function() { SyntaxHighlighter.highlight(); } ); } ); } var actionbardata = {"siteID":"25621776","siteName":"R HEAD","siteURL":"https:\/\/ctszkin.com","icon":"<img alt='' src='https:\/\/s2.wp.com\/i\/logo\/wpcom-gray-white.png' class='avatar avatar-50' height='50' width='50' \/>","canManageOptions":"","canCustomizeSite":"","isFollowing":"","themeSlug":"pub\/inove","signupURL":"https:\/\/wordpress.com\/start\/","loginURL":"https:\/\/wordpress.com\/log-in?redirect_to=https%3A%2F%2Fctszkin.com%2F2013%2F04%2F04%2Fconstruct-an-unique-index-from-two-integer-pairing-function%2F&signup_flow=account&domain=ctszkin.com","themeURL":"","xhrURL":"https:\/\/ctszkin.com\/wp-admin\/admin-ajax.php","nonce":"c7a4de0ee4","isSingular":"","isFolded":"","isLoggedIn":"","isMobile":"","subscribeNonce":"<input type=\"hidden\" id=\"_wpnonce\" name=\"_wpnonce\" value=\"1ffdf5665f\" \/>","referer":"https:\/\/ctszkin.com\/","canFollow":"1","feedID":"6029588","statusMessage":"","customizeLink":"https:\/\/rlearner.wordpress.com\/wp-admin\/customize.php?url=https%3A%2F%2Frlearner.wordpress.com%2F","i18n":{"view":"View site","follow":"Follow","following":"Following","edit":"Edit","login":"Log in","signup":"Sign up","customize":"Customize","report":"Report this content","themeInfo":"Get theme: INove","shortlink":"Copy shortlink","copied":"Copied","followedText":"New posts from this site will now appear in your <a href=\"https:\/\/wordpress.com\/read\">Reader<\/a>","foldBar":"Collapse this bar","unfoldBar":"Expand this bar","editSubs":"Manage subscriptions","viewReader":"View site in Reader","viewReadPost":"View post in Reader","subscribe":"Sign me up","enterEmail":"Enter your email address","followers":"","alreadyUser":"Already have a WordPress.com account? <a href=\"https:\/\/wordpress.com\/log-in?redirect_to=https%3A%2F%2Fctszkin.com%2F2013%2F04%2F04%2Fconstruct-an-unique-index-from-two-integer-pairing-function%2F&signup_flow=account&domain=ctszkin.com\">Log in now.<\/a>","stats":"Stats"}}; var jetpackCarouselStrings = {"widths":[370,700,1000,1200,1400,2000],"is_logged_in":"","lang":"en","ajaxurl":"https:\/\/ctszkin.com\/wp-admin\/admin-ajax.php","nonce":"6c1a73b072","display_exif":"1","display_geo":"1","single_image_gallery":"1","single_image_gallery_media_file":"","background_color":"black","comment":"Comment","post_comment":"Post Comment","write_comment":"Write a Comment...","loading_comments":"Loading Comments...","download_original":"View full size <span class=\"photo-size\">{0}<span class=\"photo-size-times\">\u00d7<\/span>{1}<\/span>","no_comment_text":"Please be sure to submit some text with your comment.","no_comment_email":"Please provide an email address to comment.","no_comment_author":"Please provide your name to comment.","comment_post_error":"Sorry, but there was an error posting your comment. Please try again later.","comment_approved":"Your comment was approved.","comment_unapproved":"Your comment is in moderation.","camera":"Camera","aperture":"Aperture","shutter_speed":"Shutter Speed","focal_length":"Focal Length","copyright":"Copyright","comment_registration":"0","require_name_email":"1","login_url":"https:\/\/rlearner.wordpress.com\/wp-login.php?redirect_to=https%3A%2F%2Fctszkin.com%2F2011%2F09%2F11%2Fcall-by-reference-in-r%2F","blog_id":"25621776","meta_data":["camera","aperture","shutter_speed","focal_length","copyright"],"local_comments_commenting_as":"<fieldset><label for=\"email\">Email (Required)<\/label> <input type=\"text\" name=\"email\" class=\"jp-carousel-comment-form-field jp-carousel-comment-form-text-field\" id=\"jp-carousel-comment-form-email-field\" \/><\/fieldset><fieldset><label for=\"author\">Name (Required)<\/label> <input type=\"text\" name=\"author\" class=\"jp-carousel-comment-form-field jp-carousel-comment-form-text-field\" id=\"jp-carousel-comment-form-author-field\" \/><\/fieldset><fieldset><label for=\"url\">Website<\/label> <input type=\"text\" name=\"url\" class=\"jp-carousel-comment-form-field jp-carousel-comment-form-text-field\" id=\"jp-carousel-comment-form-url-field\" \/><\/fieldset>","reblog":"Reblog","reblogged":"Reblogged","reblog_add_thoughts":"Add your thoughts here... (optional)","reblogging":"Reblogging...","post_reblog":"Post Reblog","stats_query_args":"blog=25621776&v=wpcom&tz=-5&user_id=0&subd=rlearner","is_public":"1","reblog_enabled":""}; ( 'fetch' in window ) || document.write( '<script src="https://s0.wp.com/wp-includes/js/dist/vendor/wp-polyfill-fetch.min.js?m=1573572739h&#038;ver=3.0.0"></' + 'ipt>' );( document.contains ) || document.write( '<script src="https://s1.wp.com/wp-includes/js/dist/vendor/wp-polyfill-node-contains.min.js?m=1540208548h&#038;ver=3.26.0-0"></' + 'ipt>' );( window.FormData && window.FormData.prototype.keys ) || document.write( '<script src="https://s1.wp.com/wp-includes/js/dist/vendor/wp-polyfill-formdata.min.js?m=1550600082h&#038;ver=3.0.12"></' + 'ipt>' );( Element.prototype.matches && Element.prototype.closest ) || document.write( '<script src="https://s2.wp.com/wp-includes/js/dist/vendor/wp-polyfill-element-closest.min.js?m=1540208548h&#038;ver=2.0.2"></' + 'ipt>' ); // <![CDATA[ (function() { try{ if ( window.external &&'msIsSiteMode' in window.external) { if (window.external.msIsSiteMode()) { var jl = document.createElement('script'); jl.type='text/javascript'; jl.async=true; jl.src='/wp-content/plugins/ie-sitemode/custom-jumplist.php'; var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(jl, s); } } }catch(e){} })(); // ]]> jQuery.extend( infiniteScroll.settings.scripts, ["jquery-core","jquery-migrate","jquery","postmessage","mobile-useragent-info","jquery_inview","jetpack_resize","spin","jquery.spin","grofiles-cards","wpgroho","syntaxhighlighter-core","syntaxhighlighter-brush-r","devicepx","jetpack_likes_queuehandler","the-neverending-homepage","wp-polyfill","blog-posts-block-view","event-countdown-js","eu-cookie-law-script","wpcom-masterbar-js","wpcom-masterbar-tracks-js","wpcom-actionbar-bar","swfobject","videopress","jetpack-carousel","tiled-gallery"] ); jQuery.extend( infiniteScroll.settings.styles, ["wpcom-smileys","wordads-global","wordads-inove","jetpack_likes","the-neverending-homepage","infinity-inove","wp-block-library","jetpack-event-countdown","jetpack-layout-grid","jetpack-ratings","jetpack-timeline","blog-posts-block-view","coblocks-frontend","posts-list-block-style","wpcom-core-compat-playlist-styles","wpcom-bbpress2-staff-css","eu-cookie-law-style","geo-location-flair","reblogging","wpcom-actionbar-bar","jetpack-global-styles-frontend-style","h4-global","wpcom-layout-grid-front","widget-author_grid","jetpack-carousel","tiled-gallery"] ); _tkq = window._tkq || []; _stq = window._stq || []; _tkq.push(['storeContext', {'blog_id':'25621776','blog_tz':'-5','user_lang':'en','blog_lang':'en','user_id':'0'}]); _stq.push(['view', {'blog':'25621776','v':'wpcom','tz':'-5','user_id':'0','subd':'rlearner'}]); _stq.push(['extra', {'crypt':'UE5XaGUuOTlwaD85flAmcm1mcmZsaDhkV11YdWtpP0NsWnVkPS9sL0ViLndld3AwazN0Jl9aRTBjRmZjZ1dJMEMvZ21relRld0JHa1R3RyVWd189LEhKY1I2SUY9cjRqTmFkb2dOMnJ2VGs/X2UyTiVQVEhlcHZQYjN3K0hrMTFsdnJRK1ExQSszRT11bUhBdmcxSHhNWjZLU01LRWUtUUZtRGRma3d8UEJ2ZHpNQk40NjRrUzdweEs0RyttSlVUdGtXeV1YYT9iOW1XPVQ9bkYmYnI1fmkxaDlTOTRXUGpWbDVfPSZZc1tNYnh0ZHpsVT1RYVdMNy0mLUZJdHArN3xoWEovK18tUV09YzZqfEJpJUk1LXpYX0R1Ly1nUg=='}]); _stq.push([ 'clickTrackerInit', '25621776', '0' ]); if ( 'object' === typeof wpcom_mobile_user_agent_info ) { wpcom_mobile_user_agent_info.init(); var mobileStatsQueryString = ""; if( false !== wpcom_mobile_user_agent_info.matchedPlatformName ) mobileStatsQueryString += "&x_" + 'mobile_platforms' + '=' + wpcom_mobile_user_agent_info.matchedPlatformName; if( false !== wpcom_mobile_user_agent_info.matchedUserAgentName ) mobileStatsQueryString += "&x_" + 'mobile_devices' + '=' + wpcom_mobile_user_agent_info.matchedUserAgentName; if( wpcom_mobile_user_agent_info.isIPad() ) mobileStatsQueryString += "&x_" + 'ipad_views' + '=' + 'views'; if( "" != mobileStatsQueryString ) { new Image().src = document.location.protocol + '//pixel.wp.com/g.gif?v=wpcom-no-pv' + mobileStatsQueryString + '&baba=' + Math.random(); } } ```