[R] how to use vectorization instead of for loop

Stephen HK WONG honkit at stanford.edu
Mon Mar 21 19:30:01 CET 2016

Dear All,

I have a dataframe like below but with many thousands rows,

structure(list(gene_id = structure(1:6, .Label = c("0610005C13Rik",
"0610007P14Rik", "0610009B22Rik", "0610009L18Rik", "0610009O20Rik",
"0610010B08Rik,OTTMUSG00000016609"), class = "factor"), log2.fold_change. = c(0.0114463,
-0.0960262, 0.00805151, -0.179981, -0.0629098, 0.155979), p_value = c(1,
0.77915, 0.98265, 0.68665, 0.85035, 0.72235), new.value = c("NA",
"NA", "NA", "NA", "NA", "NA")), .Names = c("gene_id", "log2.fold_change.",
"p_value", "new.value"), row.names = c(NA, 6L), class = "data.frame")

I want to check if second column is positive or negative value, then I will do some calculation and put the new value in last column. I can do this with for loop like below but it is not efficient. Is there a better way to use a vectorization method instead of loop? Many thanks!

for (i in 1:nrow(dataframe)) {

if dataframe[i, 2]>0 {

dataframe[i, 4]<- 1 * (1/dataframe[i,3])} else{

dataframe[i, 4] <- -1* (1/dataframe[i,3])}



Stephen H.K. WONG, PhD.

Stanford University

	[[alternative HTML version deleted]]

More information about the R-help mailing list