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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With