Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Faster for loop

I have a ~20 million row file, and I am trying to run a for-loop on it. I waited for the whole weekend but still, it wasn't even 1% done.

I am trying to make a calculation on a column of each row. If the value is <0.5 it will take and add that value to the new column if not, it will take the value and subtract it from 1, and add that value to the new column.

for (i in 1: length(halflife$year)){
  if(halflife$year[i] < 0.5){
    halflife$month[i] = halflife$year[i]
  } else{
    halflife$month[i] = 1 - halflife$year[i]
  }
}

It should add a new column with the name month to halflife, with all values less than 0.5

like image 714
cookiemonster Avatar asked Aug 20 '19 12:08

cookiemonster


1 Answers

This should be faster, using the data.table package

library(data.table)

halflife = data.table(halflife)
halflife[year < 0.5, month := year]
halflife[year >= 0.5, month := 1 - year]

Additionally, if you are reading your data from a csv file, use fread() from data.table instead of read.csv(). It's much faster

like image 155
Fino Avatar answered Sep 17 '22 14:09

Fino