0

I have the following piece of code.it is too slow right now. How can I rewrite it to improve speed? (in vectorized form , using apply functions or any other form)

my dataframe is called urban.

bcolumn.pattern <- '^b[0123456789][0123456789]'
bcolumn.index = grep(bcolumn.pattern, names(urban))
bcolumn.nrow <- dim(urban)[1]

for (k in bcolumn.index){
for (l in( 1 :bcolumn.nrow))
if (    is.nan(urban [l, ][ ,k])    )     
{urban [l, ][ ,k] <- 0 }
Hamideh
  • 651
  • 1
  • 6
  • 17
  • If you want us help optimize, we'll need a reproducible example. http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Roman Luštrik Mar 28 '14 at 10:13
  • Your subsetting is confusing. Why don't you simply use `urban[l, k]`? That would be easier to read and faster, too. But see droopy's answer for better solutions. – Roland Mar 28 '14 at 10:32

1 Answers1

2

one possibility :

# if urban is a data.frame
urban[,bcolumn.index] <- lapply(urban[,bcolumn.index], function(x) {x[is.nan(x)] <- 0; x})
# if urban is a matrix
urban[,column.index][is.nan(urban[, column.index])] <- 0
droopy
  • 2,708
  • 1
  • 12
  • 11