Regex for "is every character in string numeric?" [duplicate] - r

This question already has answers here:
Regex to check whether a string contains only numbers [duplicate]
(21 answers)
Check if string contains ONLY NUMBERS or ONLY CHARACTERS (R)
(3 answers)
Closed 4 years ago.
I'm wondering what regex pattern I could use to detect if every character in a string is numeric.
So, here's what I'm thinking that isn't working:
stringr::str_detect("311apple", "[0-9]")
#> [1] TRUE
That statement is TRUE because there exists a numeric character in that string, but I'm trying to find a pattern so that it's only TRUE if every character is numeric.
Any ideas? Thanks!

Related

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 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"

Convert string to numeric in a list in R [duplicate]

This question already has answers here:
Convert currency with commas into numeric
(4 answers)
removing particular character in a column in r
(3 answers)
Closed 4 years ago.
I am new to R. I am working on a dataset which is a large list, there is a column with numbers and strings. I want to remove the string and convert to numeric.
I have 100,000+
I want 100,000 and numeric
You can use gsub to transform the string:
as.numeric(gsub("[^0-9.]", "", "100,000+"))
# [1] 1e+05
Here, all characters, except digits and ., are removed before applying as.numeric.

R How do i split a string into a vector so that each place in the vector corresponds to a letter [duplicate]

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', ''))

Remove underscore from a string in R [duplicate]

This question already has answers here:
Replace specific characters within strings
(7 answers)
Closed 7 years ago.
In my data.frame, I have a column of type character, where all the values look like this : 123_456 (three digits, an underscore, three digits).
I need to transform these values to a numeric, and as.numeric(my_dataframe$my_column) gives me a NA. Therefore I need to remove the underscore first, in order to do as.numeric.
How would I do that please ?
Thanks
We can use sub
as.numeric(sub("_", "", my_dataframe$my_column))

Resources