Publications by Xianjun Dong
Add text aligned to legend in R plot
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(“Pearson’s r =”, R, “(p-value:”, pvalue , “)\nRMSE =”, RMSE), bty=”n”, c...
797 sym 2 img
Density curve of histogram plot in R
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)”, xlim=c(0,150), ylim=c(0,.05) )lines(density(slope, bw=1), col=”green”) Related To leave a ...
644 sym 2 img 1 tbl
ifelse function in R only returns the first element
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" "off" "off" "off" "on" "on" "on" "on" "on" Here is another nice example (from http://rwiki.sciviews.org/dok...
611 sym R (628 sym/2 pcs)
DTW: dynamic time warping 动态时间规整
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://www.rdatamining.com/examples/ts-mining)Rel...
2331 sym
single-column data frame
This is a trivial but very useful tip:> x=data.frame(a=1:4, c=5) > x a c 1 1 5 2 2 5 3 3 5 4 4 5 > x[1,] a c 1 1 5 > x[,1] [1] 1 2 3 4 > x[,1, drop=F] a 1 1 2 2 3 3 4 4where you can see that:to avoid a[, i] become a vector, rather than a single-column data frame, we can use drop=F option. Here is full explanation for that: http://sta...
673 sym R (137 sym/1 pcs)
match vs. %in%
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 more intuitive interface as a binary operator, which returns a logical vector indicating if there is ...
1238 sym R (301 sym/2 pcs)
an easy way to writing data.frame to Excel
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 Related To leave a comment for the author, please follow the link and comment on their blog: One Tip Per Day. R-blogger...
609 sym
Ternary ifelse ( ?: ) in different languages
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 just short form of if/elsecase "$b" in 5) a=$c ;; *) a=$d ;; esac Or [[ $b = 5 ]] && a="$c" || a=...
637 sym R (137 sym/4 pcs) 1 tbl
Clustering analysis and its implementation in R
Earlier I posted a blog for “k-means + heatmap” used for clustering analysis. Recently to prepare for the “Bioinformatics Tools” meeting, I made a slide with more details on “clustering analysis”. Here it is:https://docs.google.com/presentation/d/1vMS3VS3zZmT5Mty1-4R0a-1E-lstjdiXiWOeMdQ_O6M/edit Related To leave a commen...
706 sym
intersect for multiple vectors in R
Say you havea <- c(1,3,5,7,9) b <- c(3,6,8,9,10) c <- c(2,3,4,5,7,9)A straightforward way to do the job is:intersect(intersect(a,b),c)More cleverly, and more conveniently if you have a lot of arguments:Reduce(intersect, list(a,b,c))The Reduce function is part of funprog {base}, which includesReduce(f, x, init, right = FALSE, accumula...
633 sym R (296 sym/4 pcs)