November 15, 2013

String Reverse in R

String reversing is a common operation especially in Bioinformatics. However, this function is not provided in R, not even in the "stringr" package. Here you can find the code:


str_reverse <- function(x){
  return(sapply(lapply(strsplit(x, NULL), rev), paste, collapse=""))
}

s <- "abc"
s1 <- str_reverse(s)
print(s1)

November 4, 2013

Code Highlighing in Blogger

I got a relatively easy way to highlight code snippet from http://www.craftyfella.com/2010/01/syntax-highlighting-with-blogger-engine.html.

Step-1:
Add the following code in the template just above the </head> tag.
// Comment
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 




Step-2:
Write code inside <pre> tag. For example,
// Comment
public class Testing {
public Testing() {
}
 
public void Method() {
/* Another Comment
on multiple lines */
int x = 9;
}
}

The above html code gives the output as below
// Comment
public class Testing {
public Testing() {
}
 
public void Method() {
/* Another Comment
on multiple lines */
int x = 9;
}
}

I tried to make it work for R highlighting, but unfortunately it is not working. So, I am using the brush for C inside the <pre> tag.
x <- c(1, 4, -4)
m <- max(x)
print(m)

ROC (Receiver Operating Characteristics) Curve in R

There are 2 packages to calculate different measures and draw different graphs of ROC cureve.
  1. ROCR (http://rocr.bioinf.mpi-sb.mpg.de/)
  2. pROC (http://cran.r-project.org/web/packages/pROC/pROC.pdf)

Code to generate ROC curve and AUC (Area under the curve) value

Using ROCR:
 # generate ROC curve  
 pred <- prediction(scores, labels)  
 perf <- performance(pred, "tpr", "fpr")  
 plot(perf)  

 #calculate AUC value  
 aucPerf <- performance( pred, 'auc')  
 auc <- slot(aucPerf, "y.values")  

Using pROC:
 roc.obj <- roc(response=labels, predictor=scores)  
 plot(roc.obj)  
 auc(roc.obj)  

Convert a dataframe to a vector in R

It is not possible to convert a dataframe directly to a vector. However, a dataframe can first be converted to a matrix and then to a vector.

 as.vector(as.matrix(myDataFrame))