hbabbar
hbabbar

Reputation: 967

Replace value in a column using apply

I'm trying to update two columns of a huge dataset( > 45 mill rows). Basically the columns are a character format and I'm trying to convert it to a number. This is the sample data 's1':

id path convert               start                stop
1  1    0        05SEP2015:12:38:55  05SEP2015:12:38:55
2  1    0        09SEP2015:01:22:54  09SEP2015:01:22:54

I'm using the following code:

library(lubridate)

apply(s1, 1, function(row){

  s1$Start <- as.numeric(dmy_hms(row[4]))

  #Similarly code for stop column
  # s1$stop <- as.numeric(dmy_hms(row[5]

  return(s1)

})

Basically i want to convert the start text to a date and then change it to a number and then replace it in the data frame itself.

So i'm expecting an output like this :

[[1]]
id path convert       start                stop
1  1    0        1441456735  05SEP2015:12:38:55
2  1    0        1441761774  09SEP2015:01:22:54

but I'm getting :

[[1]]
id path convert       start                stop
1  1    0        1441456735  05SEP2015:12:38:55
2  1    0        1441456735  09SEP2015:01:22:54
[[2]]
id path convert       start                stop
1  1    0        1441761774  05SEP2015:12:38:55
2  1    0        1441761774  09SEP2015:01:22:54

It looks like its making a list of data frames which I don't want.

Any pointers regarding this, as I'm so near yet so far.

Upvotes: 0

Views: 263

Answers (1)

rbm
rbm

Reputation: 3253

Why not just

s1[,"start"] = as.numeric(dmy_hms(s1[,"start"]))

and

s1[,"stop"] = as.numeric(dmy_hms(s1[,"stop"]))

Upvotes: 1

Related Questions