Remove everything that doesn't begin with pattern in R [closed] - r

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have vector myvec. I would like to edit the values in the vector so that anything that doesn't begin with "NAC", I want to delete them in addition to stuffs after "_".
myvec = c("NAC1001_09ADAA", "TI09AA_NAC02111", "NACT10099_099AD")
Result I want:
NAC1001, NAC02111, NACT10099
What do I need to do for this?

We can use str_extract
library(stringr)
str_extract(myvec, '(?<=\\b|_)NACT?\\d+')
#[1] "NAC1001" "NAC02111" "NACT10099"
Or with sub from base R
sub(".*(NACT?\\d+).*", "\\1", myvec)

Split on underscore "_", then keep the one that starts with "N":
sapply(strsplit(myvec, "_"), function(i) i[ startsWith(i, "N") ])
# [1] "NAC1001" "NAC02111" "NACT10099"

Related

R regular expression to parse call option code [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I have a call option code in the form of:
.TSLA181012C100
I'd like to parse it to pull out the 18, 10 and 12. However, I'm not quite sure how to do that as the letters after the period can be of variable length and so can the numbers after the C.
Is there a regex way to find the "C" from the right and get the 6 digits to the left of that?
We can try using sub here for a base R option:
code <- ".TSLA181012C100"
num1 <- sub("^\\.[A-Z]+(\\d{2})\\d{4}C.*", "\\1", code)
num1
num2 <- sub("^\\.[A-Z]+\\d{2}(\\d{2})\\d{2}C.*", "\\1", code)
num2
num3 <- sub("^\\.[A-Z]+\\d{4}(\\d{2})C.*", "\\1", code)
num3
[1] "18"
[1] "10"
[1] "12"
This regex should work: (\d{2}){3}C or simply \d+C

How can I extract a substring in R? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have this string, for instance:
str1 = "UNCID_999277.TCGA-CV-7254-01A-11R-2016-07.111118_UNC11-SN627_0167_AD09WDACXX_TAGCTT.txt"
I would like to extract this substring, for instance:
TCGA-CV-7254
I tried something link this:
gsub(pattern = "(*.)(TCGA*)(.*)",
replacement = "\\2",
x = nameArq)
But it returns:
[1] "UNCID_999277TCGA"
Thanks for any help!
You almost had it. In the first parentheses, the period needs to come first (this means "repeat any character any number of times"). You also need some unique endpoint for the second part of your regex.
gsub(pattern = "(.*)(TCGA.*4)(.*)",
replacement = "\\2",
x = str1)

In R - how do I replace all letters in a string with other letters? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I need to anonymize names but in a very specific way so that the format of the entire string is still the same (spaces, hyphens, periods are preserved) but all the letters are scrambled. I want to consistently replace say all A's with C's, all D's with Z's, and so on. How would I do that?
We can use chartr
chartr('AD', 'CZ', str1)
#[1] "CZ,ZC. C"
data
str1 <- c('AD,DA. C')
Maybe use gsub?
string <- "ABCDEFG"
text <- gsub('A', 'C', string )
string <- gsub('D', 'Z', string )
string
[1] "CBCZEFG"

R how to cast string to a vector [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I am still a beginner in R and I can't find an answer to my question:
I use a string:
string1="c('T-shirt', 'Polo', 'Pull')"
And I need my object string1 to be a vector.
You can evaluate the expression in the string using
eval(parse(text=string1))
result:
[1] "T-shirt" "Polo" "Pull"
I am not sure what you want the final output to be. If you want string1 to be a vector of strings, the right syntax should be
string1 <- c("T-shirt", "Polo", "Pull")
Please clarify if you want a different output
You can do by both ways
eval(parse(text=string1))
or
c <- gsub("\\(|\\)|c|'", "", string1)
d <- strsplit(c,",")
e <- d[[1]]
e
This could be done with str_extract without using the eval(parse.
library(stringr)
str_extract_all(string1, "(?<=')[[:alpha:]-]+")[[1]]
#[1] "T-shirt" "Polo" "Pull"
data
string1="c('T-shirt', 'Polo', 'Pull')"

Want to add characters to every element of a data frame [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have a data frame of strings as below and would like to add the string "Market" to each of the elements of the data frame. Is there a function that would allow me to do this easily without having to use a for loop?
V1
1 PUBLIC_DISPATCHSCADA_20141221.zip
2 PUBLIC_DISPATCHSCADA_20141222.zip
3 PUBLIC_DISPATCHSCADA_20141223.zip
4 PUBLIC_DISPATCHSCADA_20141224.zip
5 PUBLIC_DISPATCHSCADA_20141225.zip
6 PUBLIC_DISPATCHSCADA_20141226.zip
We can use paste and specify the delimiter. In this case, I am using _ and pasteing the "Market" at the beginning of the string.
df1$V1 <- paste("Market", df1$V1, sep="_")
If we need to do this for each column
df1[] <- lapply(df1, function(x) paste("Market", x, sep="_"))

Resources