I have an integer
a <- (0:3)
And I would like to convert it to a character string that looks like this
b <- "(0:3)"
I have tried
as.character(a)
[1] "0" "1" "2" "3"
and
toString(a)
[1] "0, 1, 2, 3"
But neither do exactly what I need to do.
Can anyone help me get from a to b?
Many thanks in advance!
paste0("(", min(a), ":", max(a), ")")
"(0:3)"
Or more concisely with sprintf():
sprintf("(%d:%d)", min(a), max(a))
One option is deparse and paste the brackets
as.character(glue::glue('({deparse(a)})'))
#[1] "(0:3)"
Another option would be to store as a quosure and then convert it to character
library(rlang)
a <- quo((0:3))
quo_name(a)
#[1] "(0:3)"
it can be evaluated with eval_tidy
eval_tidy(a)
#[1] 0 1 2 3
Related
I have a vector of numbers of type character.
x = c("5","-.5","-.1",".01",".1","1","3")
Is there a quick and easy way to order this character vector using the numeric value of each character? I can't find a clean way to do this.
So for instance, I want a function
x <- characterOrder(x)
With output:
c("-.5","-.1",".01",".1","1","3", "5")
Thank you!
You can do this in base R using the order function and the as.numeric when you order it by the as.numeric value.
x = c("5","-.5","-.1",".01",".1","1","3")
x[order(as.numeric(x))]
[1] "-.5" "-.1" ".01" ".1" "1" "3" "5"
If you want this in a function:
characterOrder <- function(x) {
return(x[order(as.numeric(x))])
}
You could try mixedsort from gtools
library(gtools)
mixedsort(x)
#[1] "-.5" "-.1" ".01" ".1" "1" "3" "5"
i have a data like below and need to extract text comes before any number. or if we can separate the text and number then it would be great
df<-c("axz123","bww2","c334")
output
"axz", "bww", "c"
or
"axz","bww","c"
"123","2","334"
We can do:
df <- c("axz123","bww2","c334")
gsub("\\d+", "", df)
#[1] "axz" "bww" "c"
gsub("(\\D+)", "", df)
#[1] "123" "2" "334"
For your other example:
df <- "BAILEYS IRISH CREAM 1.75 LITERS REGULAR_NOT FLAVORED"
gsub("\\d.*", "", df)
#[1] "BAILEYS IRISH CREAM "
gsub("[A-Z_ ]*", "", df)
#[1] "1.75"
We can use [:alpha:] to match the alphabetic characters, and combine this with gsub() and a negation to remove all characters that are not alphabetic:
gsub("[^[:alpha:]]", "", df)
#[1] "axz" "bww" "c"
To obtain only the non-alphabetic characters we can drop the negation ^:
gsub("[[:alpha:]]", "", df)
#[1] "123" "2" "334"
Using str_extract and regex lookarounds. We match one or more characters before any number ((?=\\d)) and extract it.
library(stringr)
str_extract(df, "[[:alpha:]]+(?=\\d)")
#[1] "axz" "bww" "c"
If we need to separate the numeric and non-numeric, strsplit can be used
lst <- strsplit(df, "(?<=[^0-9])(?=[0-9])", perl=TRUE)
I am using apply to generate strings from a data frame.
For example:
df2 <- data.frame(a=c(1:3), b=c(9:11))
apply(df2, 1, function(row) paste0("hello", row['a']))
apply(df2, 1, function(row) paste0("hello", row['b']))
works as I would expect and generates
[1] "hello1" "hello2" "hello3"
[1] "hello9" "hello10" "hello11"
However, if I have
df <- data.frame(a=c(1:3), b=c(9:11), c=c("1", "2", "3"))
apply(df, 1, function(row) paste0("hello", row['a']))
apply(df, 1, function(row) paste0("hello", row['b']))
the output is
[1] "hello1" "hello2" "hello3"
[1] "hello 9" "hello10" "hello11"
Can any one please explain why I get a padded space to make all the strings the same length in the second case? I can work around the problem using gsub, but I would like to have a better understanding of why this happens
You don't need apply function:
paste0("hello", df[["a"]])
[1] "hello1" "hello2" "hello3"
paste0("hello", df[["b"]])
[1] "hello9" "hello10" "hello11"
This is happening because apply transforms your data.frame in a matrix. See what happens when you coerce df to matrix:
as.matrix(df)
a b c
[1,] "1" " 9" "1"
[2,] "2" "10" "2"
[3,] "3" "11" "3"
Notice that it coerced to a character matrix and it included the extra space on the " 9".
I have a string ,
x = "[1,2,3]"
How can I get the elements 1 and 2 from the string?
I tried the strsplit but that seems a bit tricky. Then I tried splitting on "[", and that also did not seem easy.
You could use regex lookaround to extract the numbers
library(stringr)
str_extract_all(x, '(?<=\\[|,)\\d+(?=,)')[[1]]
#[1] "1" "2"
A base option, here we just remove the brackets and split by ,, though do note #MrFlick's comment.
strsplit(gsub("\\[|\\]", "", x), ",")[[1L]][1:2]
# [1] "1" "2"
I have a dataframe in R with a column with values as "s1-112", "s10-112", "s3656-112" etc. Now i want to change the values to only the part after "s" and before "-112" that is the number after s. is there a way?
You could use gsub here
x<-c("s1-112", "s10-112", "s3656-112")
gsub("s(.*)-112", "\\1", x)
# [1] "1" "10" "3656"
Or (using #MrFlick's data)
library(stringr)
str_extract(x, perl('\\d+(?=-)'))
#[1] "1" "10" "3656"