Reputation: 21
I have a data-frame composed of 3 columns and the third column ('V3') contains the name of markers that were presented during the experiment. I'd like to add another column that tells me for each marker how many time that specific marker was presented before that instance. I have done it in R without using the tidyverse but it is quite time consuming, and I was wondering if you could help me do it using the tidyverse.
At the moment my script goes like this:
data$counter <- NA
x<-1
for (i in 1:nrow(data)){
if ((str_detect(data$V3[i], 'NEGATIVE'))==TRUE){
data$counter[i] <- x
x <- x +1
}
}
Upvotes: 0
Views: 1463
Reputation: 1810
I think cumsum
will do an excellent job here:
testdata <- data.frame(V3=sample(c("NEGATIVEsomething","others"),50,replace=TRUE), stringsAsFactors = FALSE)
negatives <- grepl("NEGATIVE",testdata$V3)
negatives <- as.numeric(negatives)
negatives <- cumsum(negatives)
negatives[negatives == 0] <- NA
testdata$counter <- negatives
Edit: Since you want to increment the counter after finding a "NEGATIVE" and place the old counter at the position, you should use
negatives <- cumsum(negatives)-1
and then remove the 0
and -1
counts at the beginning:
negatives[negatives %in% c(0,-1)] <- NA
Upvotes: 1
Reputation: 5747
You can do this with group_by()
and then generating the counters with seq_along()
.
library(dplyr)
data %>%
group_by(V3) %>%
mutate(counter = seq_along(V3)) %>%
ungroup()
Upvotes: 1