This question already has an answer here:
Add a prefix to all rows in R
(1 answer)
Closed 2 years ago.
df<- data.frame(Speaker=c('Abraham','Wassimo','Fredrick','Richard','Ravish','Rubina','Laura'),Age=c(45,47,39,3
3,36,28,30))
#data frame
df
gsub('.*^','Mr/Mrs.',df$Speaker)
results
Mr/Mrs.Abraham
Mr/Mrs.Wassimo
Mr/Mrs.Fredrick
Mr/Mrs.Richard
Mr/Mrs.Ravish
Mr/Mrs.Rubina
Mr/Mrs.Laura
I can not figure out how to add a string after the names though. Can anyone help me add a string after the names?
I don't know why you think you need a regex substitution here, just use paste:
df$Speaker <- paste0("Mr/Mrs.", df$Speaker, "text after here")
Related
This question already has answers here:
Get filename without extension in R
(9 answers)
Find file name from full file path
(4 answers)
Closed 3 years ago.
I have several download links (i.e., strings), and each string has different length.
For example let's say these fake links are my strings:
My_Link1 <- "http://esgf-data2.diasjp.net/pr/gn/v20190711/pr_day_MRI-AGCM3-2-H_highresSST_gn_20100101-20141231.nc"
My_Link2 <- "http://esgf-data2.diasjp.net/gn/v20190711/pr_-present_r1i1p1f1_gn_19500101-19591231.nc"
My goals:
A) I want to have only the last part of each string ended by .nc , and get these results:
pr_day_MRI-AGCM3-2-H_highresSST_gn_20100101-20141231.nc
pr_-present_r1i1p1f1_gn_19500101-19591231.nc
B) I want to have only the last part of each string before .nc , and get these results:
pr_day_MRI-AGCM3-2-H_highresSST_gn_20100101-20141231
pr_-present_r1i1p1f1_gn_19500101-19591231
I tried to find a way on the net, but I failed. It seems this can be done in Python as documented here:
How to get everything after last slash in a URL?
Does anyone know the same method in R?
Thanks so much for your time.
A shortcut to get last part of the string would be to use basename
basename(My_Link1)
#[1] "pr_day_MRI-AGCM3-2-H_highresSST_gn_20100101-20141231.nc"
and for the second question if you want to remove the last ".nc" we could use sub like
sub("\\.nc", "", basename(My_Link1))
#[1] "pr_day_MRI-AGCM3-2-H_highresSST_gn_20100101-20141231"
With some regex here is another way to get first part :
sub(".*/", "", My_Link1)
This question already has answers here:
How do I deal with special characters like \^$.?*|+()[{ in my regex?
(2 answers)
How to escape a question mark in R?
(4 answers)
Closed 3 years ago.
df <- data.frame(
videos = c("Moon vs Grubby", "Moon vs Happy", "Happy vs Th00"),
links = c("https://www.youtube.com/watch?v=QlNc-jb4ESk&t", "https://www.youtube.com/watch?v=VESO8YQVFSE", "https://www.youtube.com/watch?v=RI3IJT8ZzBM")
)
df$links <- as.character(df$links)
df$links <- gsub("watch?v=", "embed/", df$links)
I have got the following code with links to YouTube which I want to embed in a shiny App. However YouTube needs to replace part of the string which is interpreted as a regular expression. I did not find a helpful solution here.
So how can I gsub this pattern?
Current Links:
https://www.youtube.com/watch?v=QlNc-jb4ESk&t
Expected Outcome:
https://www.youtube.com/embed/=QlNc-jb4ESk&t
We need to escape the ? and = as these are metacharacters
gsub("watch\\?v\\=", "embed/=", df$links)
or with fixed = TRUE
gsub("watch?v=", "embed/=", df$links, fixed = TRUE)
Also, as there is only a single instance, we can use sub
sub("watch?v=", "embed/=", df$links, fixed = TRUE)
#[1] "https://www.youtube.com/embed/=QlNc-jb4ESk&t"
#[2] "https://www.youtube.com/embed/=VESO8YQVFSE"
#[3] "https://www.youtube.com/embed/=RI3IJT8ZzBM"
My guess is that this expression might work:
(\S*)watch\?v=(\S*)
The expression is explained on the top right panel of this demo, if you wish to explore further or modify it, and in this link, you can watch how it would match against some sample inputs step by step, if you like.
and our code might look like:
gsub("(\\S*)watch\\?v\\=(\\S*)", "\\1embed/\\2", df$links)
My guess is that this would be the desired output:
https://www.youtube.com/embed/QlNc-jb4ESk&t
This question already has answers here:
How do I deal with special characters like \^$.?*|+()[{ in my regex?
(2 answers)
Closed 3 years ago.
I R imports columns with no colname as ...1 I need to replace this ... with something else
Trying:
str_replace("hi_...","/././.","&")
Seems like you are trying to replace each dot . with &. You need to escape . as \\. and use str_replace_all. Try this,
library(stringr)
str_replace_all("hi_...","\\.","&")
Output,
[1] "hi_&&&"
Just in case you want to replace all three dots with & (which I barely think you wanted), use this,
str_replace("hi_...","\\.\\.\\.","&")
OR
str_replace("hi_...","\\.+","&")
Another way to achieve same can be using gsub
gsub("\\.", "&", "hi_...")
We can use
library(stringr)
str_replace("hi_...", "[.]{3}", "&")
This question already has answers here:
Test if characters are in a string
(9 answers)
Closed 4 years ago.
I need to know if there are any functions available in R that allow me to check if one string contains a substring and return a boolean. I've already tried str_detect but that doesn't suit my need.
For example:
string = 12345REFUND4567
and
substring = REFUND
contains(string,substring) would ideally return TRUE
since 12345REFUND4567 contains REFUND.
contains(string,substring) is just the format I'd imagine the function to take.
You probably are looking for grepl:
string <- "12345REFUND4567"
grepl("REFUND", string, fixed=TRUE)
[1] TRUE
This question already has answers here:
How to escape backslashes in R string
(3 answers)
Closed 5 years ago.
I’m trying to connect with a database. I use the r-package “RODBC” and the password contains a backslash. Is there a possibility to handle this problem?
library(RODBC)
channel <- odbcConnect("database", uid="theuid", pwd=”whyisherea\backslash”,
believeNRows=FALSE)
You may define your string this way
pwd_string <- "whyisherea\\backslash"