# Articles by Xianjun Dong

### Ternary ifelse ( ?: ) in different languages

February 23, 2012 |

AWK\$ awk 'ORS=NR%3?",":"\n"' student-marksPerl /PHP\$result = (\$a __ \$b) ? \$x : \$y;In Per6, use double ? and ! instead.\$result = (\$a __ \$b) ?? \$x !! \$y;Rifelse(a__0,a,0)Ternary operator (if?true:false)bash/linuxternary operator ? : is ju... [Read more...]

### an easy way to writing data.frame to Excel

November 16, 2011 |

you can write it aswrite.table(r.data.frame, "excel.file.xls", sep="\t", na="", row.names=F)which I can usually open in Excel just by clicking on it.Credit: http://tolstoy.newcastle.edu.au/R/help/05/04/3388.html [Read more...]

### match vs. %in%

November 4, 2011 |

match and %in% are two very commonly-used function in R. So, what's the difference of them?First, how to use them -- (copy from R manual)match returns a vector of the positions of (first) matches of its first argument in its second.%in% is a ... [Read more...]

### single-column data frame

October 10, 2011 |

This is a trivial but very useful tip:__ x=data.frame(a=1:4, c=5)__ x a c1 1 52 2 53 3 54 4 5__ x[1,] a c1 1 5__ x[,1][1] 1 2 3 4__ x[,1, drop=F] a1 12 23 34 4where you can see that:to avoid a[, i] become a vector, rather than a... [Read more...]

### DTW: dynamic time warping 动态时间规整

September 18, 2011 |

Basically, DTW (dynamic time warping) is an algorithm to output cumulative distance of two time sequences, which is widely used e.g. for classification and clustering.For example, when using k-mean for clustering, we can use DTW as distance function. Here is one of such nice instances (using R: http://... [Read more...]

### ifelse function in R only returns the first element

September 16, 2011 |

If you also favor to use the function, be aware of the returned value. For example:__ ifelse(1__0, 3, 4) [1] 3__ ifelse(1__0, c(2, 3), c(4, 5)) # only the first element returned.[1] 2 __ ifelse(c(1:10)__5, 'on', 'off') [1] "off" "off... [Read more...]

### Density curve of histogram plot in R

September 1, 2011 |

Ref: http://casoilresource.lawr.ucdavis.edu/drupal/book/export/html/23 To add density curve on a histogram, like the green curve above, use code below:#plot the distributionhist(slope, breaks=1000, freq=F, main=main, xlab="Slope Value (percent)", ... [Read more...]

### Add text aligned to legend in R plot

September 1, 2011 |

What I meant is to add text on a R plot when there is already legend on it. Like the left plot in above figure, another piece of text was put exactly below the legend "Pearson'r ...RMSE = 1.9". Here is the code for that: l=legend("topleft", paste(... [Read more...]
1 2 3