Reputation: 11
I would like to create a table or a new data frame that displays, for each row in the original data frame, how many times a specific value precedes another specific value. For example, if I have the following data frame:
x <- data.frame("Red" = c("a", "b", "a", "a", "c", "d"), "Blue" = c("b", "a", "b", "a", "b", "a"), "Green" = c("a", "a", "b", "a", "b", "a"))
and I want to know, for each color (Red, Blue, and Green) how many times the sequence "b", "a" occurs (i.e., how many times b precedes a in the sequence).
The correct answer would look something like this:
Color ba
1 Red 1
2 Blue 3
3 Green 2
Upvotes: 0
Views: 45
Reputation: 420
here is one solution using stringr
library(stringr)
count_pair <- function(x, pattern) {
p <- paste(pattern, collapse = "")
s <- paste(x, collapse = "")
str_count(s, pattern = p)
}
z <- apply(x, 2, count_pair, pattern = c("b", "a"))
# Red Blue Green
# 1 3 2
# if you want the output in form of a data.frame you could run:
df <- as.data.frame(as.table(z))
# Var1 Freq
# 1 Red 1
# 2 Blue 3
# 3 Green 2
Upvotes: 1