Expression using greek letters in R
3
Hello,
I'm trying to detect greek letters in a string in R and convert my string to an expression. I know there are multiple threads about this (https://stackoverflow.com/questions/6044800/adding-greek-character-to-axis-title) . But I can't find a way to paste
multiple greek letters at once...
My data :
str <- "myrangammadomsequenmuce15874withsomephigreek2874lettergamma"
I want to get something like this :
expression(paste("myran", gamma, "domsequen", mu, "ce15874withsome", phi, "greek2874letter", gamma)
I tried to split my string using multiple delimiters but when I want to collapse I don't know which was the delimiter used.
Thanks !
R
• 11k views
After 1 day I've found a really dirty way !
library(stringr)
str <- "myrangammadomsequenmuce15874withsomephigreek2874lettergamma"
str <- gsub("gamma", "'~gamma~'", str)
str <- gsub("phi", "'~phi~'", str)
str <- gsub("mu", "'~mu~'", str)
str
#[1] "myran'~gamma~'domsequen'~mu~'ce15874withsome'~phi~'greek2874letter'~gamma~'"
if (substr(str,1,1) == "'"){
str <- paste0("expression(",substr(str,3,nchar(str)))
} else{
str <- paste0("expression('",str)
}
if (str_sub(str,-1,-1) == "'"){
str <- paste0(substr(str,1,nchar(str)-2),")")
} else{
str <- paste0(str,"')")
}
result <- eval(parse(text=str))
result
#expression("myran" ~ gamma ~ "domsequen" ~ mu ~ "ce15874withsome" ~ phi ~ "greek2874letter" ~ gamma)
I don't have the full answer, but below should get you started:
library(stringr)
x <- "myrangammadomsequenmuce15874withsomephigreek2874lettergamma"
greek <- "gamma|mu|phi"
str_split(x, greek)
# [[1]]
# [1] "myran" "domsequen" "ce15874withsome" "greek2874letter" ""
str_extract_all(x, greek)
# [[1]]
# [1] "gamma" "mu" "phi" "gamma"
str_locate_all(x, greek)
# [[1]]
# start end
# [1,] 6 10
# [2,] 20 21
# [3,] 37 39
# [4,] 55 59
can you get greek letters converted to uTF(8) in R (from english words to greek to alphabets)? If so, you can use lookaround in R.
>gamma=intToUtf8(0x03B3)
>mu=intToUtf8(0x03BC)
>phi=intToUtf8(0x03D5)
> test1=paste0("myran", gamma, "domsequen", mu, "ce15874withsome", phi, "greek2874letter", gamma)
> test1
[1] "myranγdomsequenμce15874withsomeϕgreek2874letterγ"
> strsplit(test1,"(?<=\\W)(?=\\w)|(?<=\\w)(?=\\W)|(?<=\\W)(?=\\W)", perl = T)
[[1]]
[1] "myran" "γ" "domsequen" "μ" "ce15874withsome"
[6] "ϕ" "greek2874letter" "γ"
Login before adding your answer.
Traffic: 1618 users visited in the last hour
@Bastien: Pure R questions like this are best asked elsewhere.
Edit: Perhaps there is a bioinformatics angle. Are you trying to label an axis with a greek character?
I'm trying to display labels for IgH locus of mm10 (gamma3, gamma1, mu etc...)
I have GRanges with these locus as name and I want to create an expression vector with it
Probably would be better if you could share example GRanges object, and expected output.
Why do you need the entire GRanges ?
In my GRanges, first line I have as name :
myrangammadomsequenmuce15874withsomephigreek2874lettergamma
I want to create an expression with that and append this expression to a vector of expression...etc
Then second line, I take the name, then I append the vector of expression.
I know how to loop over my GRanges and append a vector. I'm just stuck at expression creation
OK, still wondering how did you end up with that string.
I'll ask users to input their own string. As everyone know here, user are unpredictable. I expect this string will be the most wtf string I will got from my users :)
How did you come up with that string, where is it from?
my
str
string is pure training but covers all cases.