Question

I'm trying to do the following:

Evaluate a POSIXct class column in a data frame, if an observation is an even second do nothing, and if its an odd second add 1.

This is what I have so far:

df[,1] <- lapply(df[,1], function(x) ifelse(as.integer(x)%%2==0, yes = x, no = x+1))

At this point my computer is running out of memory, probably because of the yes = x in the ifelse function... But I'm not sure.

Is there a better way to approach this problem?

Note: There are around 150,000 obs.

Edit: Here is a sample of my data:

             Timestamp   PSTC01 PSTC02 PSTC03 PSTC04   PSTC05   PSTC06   PSTC07 PSTC08   PSTC09   PSTC10   PSTC11
1  2013-09-02 23:56:02 0.225339     NA     NA     NA       NA 0.222298 0.253884     NA 0.243435       NA       NA
2  2013-09-02 23:56:32 0.220459     NA     NA     NA       NA 0.220009 0.250797     NA 0.241659       NA       NA
3  2013-09-02 23:57:02 0.218379     NA     NA     NA       NA 0.216663 0.252008     NA 0.240208       NA       NA
4  2013-09-02 23:57:32 0.218264     NA     NA     NA       NA 0.215935 0.256784     NA 0.240165       NA       NA
5  2013-09-02 23:58:02 0.222438     NA     NA     NA       NA 0.220964 0.253382     NA 0.241622       NA       NA
6  2013-09-02 23:58:32 0.222154     NA     NA     NA       NA 0.222533 0.252455     NA 0.242187       NA       NA
7  2013-09-02 23:59:02 0.223612     NA     NA     NA       NA 0.226128 0.253611     NA 0.243376       NA       NA
8  2013-09-02 23:59:32 0.221370     NA     NA     NA       NA 0.225215 0.253617     NA 0.243793       NA       NA
9  2013-09-06 00:00:01 0.268708     NA     NA     NA 0.207481       NA 0.277915     NA       NA 0.241519 0.242069
10 2013-09-06 00:00:31 0.268708     NA     NA     NA 0.207481       NA 0.277915     NA       NA 0.241519 0.242069
11 2013-09-06 00:01:01 0.265310     NA     NA     NA 0.218782       NA 0.269295     NA       NA 0.236030 0.211069
12 2013-09-06 00:01:31 0.270377     NA     NA     NA 0.217813       NA 0.272507     NA       NA 0.236714 0.219648
13 2013-09-06 00:02:01 0.271845     NA     NA     NA 0.213403       NA 0.271097     NA       NA 0.236685 0.218460
Was it helpful?

Solution

df[1] <- df[1] + as.integer(df[[1]]) %% 2

will be much faster due to vectorized operations. By the way: You don't need yes explicitly in ifelse, but it doesn't affect speed.

The above command adds one second to all odd values. If you want to do the opposite, i.e., add one second to even values, you just have to insert the logical not operator, !:

df[1] <- df[1] + !as.integer(df[[1]]) %% 2

OTHER TIPS

Try

df[df[,1] %% 2 == 0,1] <- df[df[,1] %% 2 == 0,1] + 1

EDIT:

After reading your comment is looks like Sven's idea would work best. Try this (and adopting Sven's approach):

df[,1] <- df[,1] + {as.integer(df[,1]) %% 2}

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top