R - substring by number [duplicate] - r

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"

Related

What is the R function and code for summing a list of characters in R? [duplicate]

This question already has answers here:
Count number of distinct values in a vector
(6 answers)
Closed 1 year ago.
I am stuck on a question in R, i need to sum a list of characters from the starwars dataset, which is the starwars eye color. The question asks how many different eye colours the character have. The answer is 15, which i derived from a table (table(starwars$eyecolor), but i cannot figure the code to get to 15.
Try any of below
> length(levels(factor(starwars$eye_color)))
[1] 15
> length(unique(starwars$eye_color))
[1] 15
> sum(!duplicated(starwars$eye_color))
[1] 15
length(unique(starwars$eye_color))

R character string methods [duplicate]

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

How can I extract the four first numbers from a variable? R [duplicate]

This question already has answers here:
Extract the first (or last) n characters of a string
(5 answers)
Closed 2 years ago.
Well, I got a variable that shows numbers like this one: 13015064000992062, and I need to extract the four first numbers and put it in another column. In this case, it will be 1301.
A friend told me that it was using stringr library, but he can't remember the code.
I am working with a data frame. So I need to get a new variable called "canton", that are the four first digits from 'Identificador'
So it looks like this:
We can use substr
permMan19$newcol <- substr(permMan19$identificador, 1, 4)

How to extract similar element from a vector? [duplicate]

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"

Accessing the last 5 objects of a particular column in a dataframe in R [duplicate]

This question already has answers here:
Getting the last n elements of a vector. Is there a better way than using the length() function?
(6 answers)
Closed 5 years ago.
I am working in a dataframe in R and I want to access the last 5 objects of a particular column in a dataframe.How do i go about it??
One option is tail(dataframe$column, 5)

Resources