This question already has answers here:
How to calculate the number of occurrence of a given character in each row of a column of strings?
(14 answers)
Count the number of pattern matches in a string
(6 answers)
Closed 2 years ago.
Say I have a strings
seq1 <- "ACTACTGGATGACT"
pattern1 <- "ACT"
What is the best way to find the number of times the pattern is in the sequence, in R? I would like to use a sliding window for loop, but im not clear on the proper way to handle the character strings.
We can use str_count
library(stringr)
str_count(seq1, pattern1)
#[1] 3
Related
This question already has answers here:
string starting with specific pattern
(2 answers)
Filter a vector of strings based on string matching
(4 answers)
Closed 3 years ago.
I have a vector which have following three strings
test <- c("www.example.com/topic/university-admission",
"www.example.com/topic/school-admission",
"www.example.com/college-admission")
I want to extract all the element which have pattern like this "www.example.com/topic/"
One way is just looking at vector and extract as per the index location of that element.
But this will be challenging if vector has length of say 100.
Is there any way to do this using string pattern matching?
test[startsWith(x = test, "www.example.com/topic/")]
# [1] "www.example.com/topic/university-admission" "www.example.com/topic/school-admission"
This question already has answers here:
How to generate all possible combinations of vectors without caring for order?
(2 answers)
Closed 3 years ago.
I have multiple character strings, let's say 'pred_1', 'pred_2' and 'pred_3'. Now I want to get a list with all pairs of the strings. So, the resulting list should contain 'pred_1 pred_2', 'pred_1 pred_3' and 'pred_2 pred_3'. Does anyone know how to automate this for more than three character strings?
An option is combn
combn(v1, 2, simplify = FALSE)
data
v1 <- paste0("pred_", 1:3)
This question already has answers here:
stringr extract full number from string
(1 answer)
Extracting unique numbers from string in R
(7 answers)
Extracting numbers from vectors of strings
(12 answers)
Closed 3 years ago.
I have
strQuestions <- c("Q1", "Q22")
I need to get
nQuestions <- c(1,22)
How can I do it nicely? (with stringr?). Thank you
With stringr:
str_sub(strQuestions, 2, 3)
stringr::str_remove_all(strQuestions,"[A-Za-z]")
You can add the function as.numeric() after.
This question already has answers here:
Extracting numbers from vectors of strings
(12 answers)
Closed 3 years ago.
what is the most easiest way how to get number from string? I have huge list of links like this, I need to get that number 98548 from it.
https://address.com/admin/customers/98548/contacts
Note that number cant have different count of numbers and can start from 0 to 9
This is the most easiest that I know :
str <- "https://address.com/admin/customers/98548/contacts"
str_extract_all(str, "\\d+")[[1]]
Using stringr:
no="https://address.com/admin/customers/98548/contacts"
unlist(stringr::str_extract_all(no,"\\d{1,}"))
[1] "98548"
This question already has answers here:
Split a character vector into individual characters? (opposite of paste or stringr::str_c)
(4 answers)
Closed 4 years ago.
I am trying to split a string let's say "abcde" into a vector of "a","b","c","d","e"
How can i do that?
i have tried strsplit but that makes it into 1 element
a=unlist(strsplit("abcde", split=" "))
We need the split = ""
unlist(strsplit('abcde', ''))