R split column depending on values -


i have following data , want calculate total number of minutes , wondering if possible split column 2 minutes in 1 column , seconds in column?

> q        time 1   0m 22s  2    1m 7s  3   3m 35s  4  11m 43s  5    1m 8s  6   2m 21s  7   9m 33s  8   0m 56s  9    0m 2s  10   0m 2s  11  0m 50s  12  0m 25s  13  0m 33s  14  2m 26s  15  0m 20s  16  1m 47s  17  0m 36s  18   0m 3s  19   0m 2s  20   0m 5s  

==> give:

> q     min    seconds 1   0     22 2   1     7 

etc

i not familiar dates can functions as.date or strptime. using data.frame:

df <- data.frame(time = c("0m 22s", "1m 7s", "3m 35s", "11m 43s", "1m 8s", "2m 21s", "9m 33s", "0m 56s", "0m 2s", "0m 2s", "0m 50s", "0m 25s", "0m 33s", "2m 26s", "0m 20s", "1m 47s", "0m 36s", "0m 3s", "0m 2s", "0m 5s"))  df$time.2 <- strptime(df$time, "%mm %ss") 

now can select specific values, take @

attributes(df[, "time.2"]) 

and assign

df$min <- df[, "time.2"][["min"]] df$sec <- df[, "time.2"][["sec"]] 

this gives:

r> df       time              time.2 min sec 1   0m 22s 2010-12-02 00:00:22   0  22 2    1m 7s 2010-12-02 00:01:07   1   7 3   3m 35s 2010-12-02 00:03:35   3  35 4  11m 43s 2010-12-02 00:11:43  11  43 5    1m 8s 2010-12-02 00:01:08   1   8 6   2m 21s 2010-12-02 00:02:21   2  21 7   9m 33s 2010-12-02 00:09:33   9  33 8   0m 56s 2010-12-02 00:00:56   0  56 9    0m 2s 2010-12-02 00:00:02   0   2 10   0m 2s 2010-12-02 00:00:02   0   2 11  0m 50s 2010-12-02 00:00:50   0  50 12  0m 25s 2010-12-02 00:00:25   0  25 13  0m 33s 2010-12-02 00:00:33   0  33 14  2m 26s 2010-12-02 00:02:26   2  26 15  0m 20s 2010-12-02 00:00:20   0  20 16  1m 47s 2010-12-02 00:01:47   1  47 17  0m 36s 2010-12-02 00:00:36   0  36 18   0m 3s 2010-12-02 00:00:03   0   3 19   0m 2s 2010-12-02 00:00:02   0   2 20   0m 5s 2010-12-02 00:00:05   0   5 

edit: since want split data.frame in order able calculate total sum of minutes, not create new columns min , sec , can work column time.2. 2 steps enough

df$time.2 <- strptime(df$time, "%mm %ss") sum(df[, "time.2"][["min"]])  r> [1] 30 

Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -