How to extract number from string? [duplicate] - r

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.

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 do i round numbers in a column in R? [duplicate]

This question already has answers here:
Formatting Decimal places in R
(15 answers)
Closed 2 years ago.
How do I round the numbers to 2 decimal points within a column within a dataframe?
The name of the df is tax_data and the column that I want to round is called rate_percent
I tried using:
format(round(rate_percent ,2), nsmall =2) but this didn't work.
Does anyone have any suggestions?
Here, in Base-R
tax_data$rate_percent <- round(tax_data$rate_percent, 2)

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

Is there any way to turn a column into a string? [duplicate]

This question already has answers here:
How do I paste string columns in data.frame [duplicate]
(2 answers)
Closed 5 years ago.
For example, if I have a data set
data.frame(x = c("a","b","a","b","c"))
how can I get a output =
a,b,a,b,c in a list or text.
paste(data_frame,collapse=",") will collapse the column into a single string.

The most efficient way to calculate numbero of unique values in vector [duplicate]

This question already has answers here:
How to count the number of unique values by group? [duplicate]
(1 answer)
Unique values in each of the columns of a data frame
(9 answers)
Closed 5 years ago.
Is there better way to compute this instead of:
length(unique(vector))
let's assume that we donno what class is the vector.
You can use
library(data.table)
vector <- c(1,2,2,2,3)
uniqueN(vector)

Resources