Re: [R] how to use vectorization instead of for loop

2016-03-21 Thread Dalthorp, Daniel
> > > From: ruipbarra...@sapo.pt <ruipbarra...@sapo.pt> > > Sent: Monday, March 21, 2016 11:50 AM > > To: Stephen HK WONG > > Cc: r-help@r-project.org > > Subject: Re: [R] how to use vectorization instead of for loop &g

Re: [R] how to use vectorization instead of for loop

2016-03-21 Thread ruipbarradas
rom: ruipbarra...@sapo.pt <ruipbarra...@sapo.pt> > Sent: Monday, March 21, 2016 11:50 AM > To: Stephen HK WONG > Cc: r-help@r-project.org > Subject: Re: [R] how to use vectorization instead of for loop > > Hello, > > I've renamed your dataframe to 'dat'. Since ?ifelse i

Re: [R] how to use vectorization instead of for loop

2016-03-21 Thread Stephen HK WONG
ifelse ? Thanks. From: ruipbarra...@sapo.pt <ruipbarra...@sapo.pt> Sent: Monday, March 21, 2016 11:50 AM To: Stephen HK WONG Cc: r-help@r-project.org Subject: Re: [R] how to use vectorization instead of for loop Hello, I've renamed your dataframe to 'dat'. Since ?ifelse is vectorized, try

[R] how to use vectorization instead of for loop

2016-03-21 Thread Stephen HK WONG
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,OTTMUSG0016609"), class = "factor"), log2.fold_change. = c(0.0114463,

Re: [R] how to use vectorization instead of for loop

2016-03-21 Thread ruipbarradas
Hello, I've renamed your dataframe to 'dat'. Since ?ifelse is vectorized, try dat[, 4] <- ifelse(dat[, 2] > 0, 1 * (1/dat[,3]), -1* (1/dat[,3])) Oh, and why do you multiply by 1 and by -1? It would simply be 1/dat[,3] and -1/dat[,3]. Hope this helps, Rui Barradas Quoting Stephen HK WONG