R how to cast string to a vector [closed] - r

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

Related

Remove everything that doesn't begin with pattern in R [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 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"

Extract one column from tableone [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 3 years ago.
Improve this question
I use the tableone package in r and it is extremely usefull.
I was wandering if someone was able to easily extract one column from it for convenient use.
Lets say just take column 1 and put it in a data frame that has
"Name","Value","Value in parenthesises" columns.
Thanks
(Kindly ignore the numbers here, they are just for demonstration purposes.)
Thanks :)
To do what you seem to want to do you might use str_extract from the stringr package. It works like this:
If this is the kind of data you have in your columns:
data <- c("1234 (567.8)", "4321 (12.34)", "5678 (91.234)")
then install the package and call it:
install.packages("stringr")
library(stringr)
and define regular expressions for what is to go into the columnValueand what is to go into column ValueInParentheses:
df <- data.frame(
Value = str_extract(data, "\\w.*(?=\\()"),
ValueInParentheses = str_extract(data, "(?<=\\()\\w.*(?=\\))")
); df
Value ValueInParenthesis
1 1234 567.8
2 4321 12.34
3 5678 91.234

Practice Exercise on tidyr Functions [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 3 years ago.
Improve this question
Using the first.df data frame, separate the DoB column data into 3 new columns - date, month,year by using the separate() function.I tried last line but it is not giving desired result.
fname <- c("Martina", "Monica", "Stan", "Oscar")
lname <- c("Welch", "Sobers", "Griffith", "Williams")
DoB <- c("1-Oct-1980", "2-Nov-1982", "13-Dec-1979", "27-Jan-1988")
first.df <- data.frame(fname,lname,DoB)
print(first.df)
separate(first.df,DoB,c('date','month','year'),sep = '-')
Moved my comment to an actual answer.
To retain the date column you need to add the remove = FALSE parameter, and to discard one of the separated columns simply add NA instead of a column name. The correct command is then
separate(first.df,DoB,c(NA,'month','year'),sep = '-', remove=FALSE)

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"

Resources