This question already has an answer here:
grepl not searching correctly in R
(1 answer)
Closed 6 years ago.
I want to know if there is a certain string contained in another string. This works fine here:
grepl("a","a")
However, what I actually want to test is the following and this one doesn't work:
grepl("is.na(x)","is.na(x)")
Can anyone help?
You can escape the special characters like this:
grepl("is\\.na\\(x\\)","is.na(x)")
[1] TRUE
Related
This question already has answers here:
How to use the strsplit function with a period
(3 answers)
Closed 5 years ago.
I apologize for possible similar questions, but I just can't find the solution for my problem. So, I have a string with three parts, separated by “.”, for example:
a <- "XXX.YY.ZZZ"
(the length of strings differ, it could also be a <- "XXXX.Y.ZZ", but the three parts are always separated by the two “.”.
I solved the problem for the first part:
library(stringi)
stri_extract(a, regex='[^.]*')
[1] "XXX"
Appreciate your help.
hello you can use strsplit as follows
strsplit(a,"\\.")[[1]]
This question already has answers here:
Creating a comma separated vector
(6 answers)
Closed 6 years ago.
I am trying to create a comma separated string exactly like this to put in a url: 1,2,3,4,5 but when I try: paste0(1:5,sep=",") it returns: [1] "1," "2," "3," "4," "5," The spaces are causing issues. How do I get rid of them?
This is not the same question as: this SO post the solution is almost the same, but the question approaches it from a different angle. My question is trying to suppress the spaces, the linked question wants them.
Try to use collapse="," instead of sep=",":
paste0(1:5,collapse=",")
[1] "1,2,3,4,5"
This question already has answers here:
grepl for a period "." in R?
(2 answers)
Closed 6 years ago.
I am trying to grep all the records of a dataframe which has a full stop in it. But it returns all the records in the dataframe no matter it has a full stop or not.
dot=e[grep(".", e$e1),]
View(dot)
Can anyone help me to do this in the correct way please?
You can either use the fixed=True argument, or use "\."
dot=e[grep(".", e$e1, fixed=T),]
or
dot=e[grep("\\.", e$e1),]
The first option is probably preferable.
This question already has answers here:
How can I paste 100000 without it being shortened to 1e+05? [duplicate]
(3 answers)
Closed 9 years ago.
The result of function was displayed in scientific notation, I want to change it back to normal, but only for that function, I don't want to change the global setting. Can anyone help?
You can do:
format(functionResult, scientific=FALSE);
or:
as.integer(functionResult);
This question already has answers here:
Comma separator for numbers in R?
(4 answers)
Closed 4 years ago.
I often use RStudio for a simple calculations, something like the following
> (3.456+4.008)*13000+1000000
[1] 1097032
And my question (rather stupid, I assume, but I haven't find an answer) is if it's possible to make the output more friendly? E.g.:
> (3.456+4.008)*13000+1000000
[1] 1'097'032
At the prompt:
?format
As an example
format(12345.6789, digits=9, decimal.mark=",");