Reputation: 157
I have a dataframe including sample names, measurement modes and starting times of measurement. The measurements are continuous and each sample is sequentially measured using all the modes.
##example
df <- data.frame(sample= rep(c(1:10),each = 4,length.out=100),
mode =rep_len(c("A","B","C","D"),length.out = 100),
time= seq(strptime("2018-1-1 0:0:0","%Y-%m-%d %H:%M:%S"),by= dseconds(200),length.out=100))
For each sample I need to get an ID composed by the sample name and the starting time of mode "B". I grouped it by sample using:
group <- function(x) cumsum(c(1, diff(x) != 0))
df$group <- group(df$sample)
then I tried to fill the new column ID
using
library(dplyr)
df %>%
group_by(group) %>%
mutate(ID = paste(df$sample,
as.character(substr(df$time, start = 12,stop=16)),sep="_"))
however this code doesn't work and in addition it would paste all the starting times and not the ones corrisponding to the mode "B".
Upvotes: 1
Views: 534
Reputation: 174
In your mutate command you do not need to reference your dataframe (df). You only need to reference the column ID. I believe this code will work for you.
df%>%
group_by(group)%>%
mutate(ID = paste(sample,as.character(substr(time, start = 12,stop=16)),sep="_"))
Upvotes: 1