This question already has answers here:
Concatenate a vector of strings/character
(8 answers)
Closed 4 years ago.
For the following operation:
avector<-c("p1","p2","p3")
Reduce(paste,avector)
## "p1 p2 p3"
I want to get "p1.p2.p3"
Which is applying the paste function in Reduce with separator "."
Please advice.
Try this paste(avector,collapse = ".")
Related
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:
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', ''))
This question already has answers here:
Learning Regular Expressions [closed]
(1 answer)
Extracting a string between other two strings in R
(4 answers)
Closed 4 years ago.
I have character strings like
str1 <- "Hello {can you please} {extract this}"
I need to extract "can you please" and "extract" this as a list with regex.
How can I do it?
This question already has answers here:
Remove all text before colon
(10 answers)
Closed 5 years ago.
I've got a table of strings like i.e.:
START0001?sthEND1,
START002?sthEND2,
START03?sthEND3,
START4
How could I obtain the table:
START1,
START2,
START3,
START4
?
I can do this with
gsub(sub('^([^?]+)*','',napis),"",napis)
but there is a problem with "?" sign that stays after all.
Try this:
y<-"START0001?sthEND1"
x<-unlist(strsplit(y,""))
idx<-which(grepl("\\?",x))
x<-paste0(x[1:(idx-1)],collapse = "")
x<-gsub("0","",x)
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.