How to make complicated transformation of string in column based on value of other column?

I have a dataframe:

ID          time             value       operation
K1   2020-10-12 07:35:47    K1-0735       create
K1   2020-10-12 07:35:49    K1-0735       upload
K1   2020-10-12 07:35:50    
K1   2020-10-12 07:35:55    K1-0735       create   
K1   2020-10-12 07:35:58    K1-0735       upload
K1   2020-10-12 07:37:19    
KK   2020-10-13 08:11:09    KK-0811       create
KK   2020-10-13 08:11:09    KK-0811       create
KK   2020-10-13 08:11:12    KK-0811       upload       
KK   2020-10-13 08:11:15
KK   2020-10-13 08:11:25    KK-0811       create
KK   2020-10-13 08:11:26    KK-0811       upload  

As you see column "value" is a join of value of column "ID" and hour and minute from "timestamp" without spaces. I want to add seconds to that value to make it unique within a minute. However they must have same seconds of they are in bundle of "operation" column of values create and upload.

So desired result is:

ID          time             value         operation
K1   2020-10-12 07:35:47    K1-073547       create
K1   2020-10-12 07:35:49    K1-073547       upload
K1   2020-10-12 07:35:50    
K1   2020-10-12 07:35:55    K1-073555       create   
K1   2020-10-12 07:35:58    K1-073555       upload
K1   2020-10-12 07:37:19    
KK   2020-10-13 08:11:09    KK-081109       create
KK   2020-10-13 08:11:09    KK-081109       create
KK   2020-10-13 08:11:12    KK-081109       upload       
KK   2020-10-13 08:11:15
KK   2020-10-13 08:11:25    KK-081125       create
KK   2020-10-13 08:11:26    KK-081125       upload  

How could i do that transformation with the type of condition?

Code to build dataframe:

ID <- c("K1","K1","K1","K1","K1","K1","KK","KK","KK","KK","KK","KK")

time <- as.Date(c('2020-10-12 07:35:47','2020-10-12 07:35:49','2020-10-12 07:35:50',
                     '2020-10-12 07:35:55','2020-10-12 07:35:58', '2020-10-12 07:37:19',
                     '2020-10-13 08:11:09','2020-10-13 08:11:09','2020-10-13 08:11:12',
                     '2020-10-13 08:11:15','2020-10-13 08:11:25','2020-10-13 08:11:26'))

value <- c("K1-0735","K1-0735",NA,"K1-0735","K1-0735",NA,"KK-0811","KK-0811","KK-0811",
           NA,"KK-0811","KK-0811")

operation <- c("create", "upload", NA,"create", "upload", NA,"create","create", "upload",
               NA,"create", "upload")
data <- data.frame(ID, time, value,operation)


Read more here: https://stackoverflow.com/questions/65718799/how-to-make-complicated-transformation-of-string-in-column-based-on-value-of-oth

Content Attribution

This content was originally published by french_fries at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: