Paste together two character vectors of different lengths - r

I have two different character vectors in R, that I want to combine to use for column names:
groups <- c("Group A", "Group B")
label <- c("Time","Min","Mean","Max")
When I try using paste I get the result:
> paste(groups,label)
[1] "Group A Time" "Group B Min" "Group A Mean" "Group B Max"
Is there a simple function or setting that can paste these together to get the following output?
[1] "Group A Time" "Group A Min" "Group A Mean" "Group A Max" "Group B Time"
[6] "Group B Min" "Group B Mean" "Group B Max"

Probably outer helps your work. Try this:
> c(t(outer(groups, label, paste)))
[1] "Group A Time" "Group A Min" "Group A Mean" "Group A Max" "Group B Time" "Group B Min"
[7] "Group B Mean" "Group B Max"

outer
outer(groups, labels, FUN=paste)

Since it's two element array, I would do
c(paste(groups[1],label),paste(groups[2],label))

Related

R capitalize roman numerals only in string

If I have a vector with the following:
people <- c("PERSON I", "PERSON II", "PERSON III", "PERSON IV")
To turn them into title case, I used the following:
people <- str_to_title(people)
Now I have the following
> people
[1] "Person I" "Person Ii" "Person Iii" "Person Iv"
What do I to capitalize the Roman numerals only like this?
"Person I" "Person II" "Person III" "Person IV"
Or is there a way to convert the all-caps into the last vector without using str_to_title?
Here is a base R option using substr, sub, and paste:
people <- c("PERSON I", "PERSON II", "PERSON III", "PERSON IV")
people <- paste0(substr(people, 1, 1), tolower(sub("^\\S(\\S+).*$", "\\1", people)),
" ", sub("^.*?(\\S+)$", "\\1", people))
people
[1] "Person I" "Person II" "Person III" "Person IV"
If it's always the same format, i.e. two words separated by a space, you can use the following:
sapply(strsplit(people,' '), function(X){paste(str_to_title(X[1]), X[2]) })

R all permutations per vector entry

I have a vector
x <- c("a b c", "d e")
with splitted entries
str_split(x, " ")
I want to get all permutations per splitted vector entry, so the result should be
c("a b c", "b c a", "c a b", "a c b", "b a c", "c b a", "d e", "e d")
I tried to use function
permutations(n, r, v=1:n, set=TRUE, repeats.allowed=FALSE)
After the str_split step , you can use combinat::permn to create all possible permutation of the string and paste them together.
result <- unlist(sapply(strsplit(x, " "), function(x)
combinat::permn(x, paste0, collapse = " ")))
result
#[1] "a b c" "a c b" "c a b" "c b a" "b c a" "b a c" "d e" "e d"
You can try pracma::perms like below
unlist(
Map(
function(v) do.call(paste, as.data.frame(pracma::perms(v))),
strsplit(x, " ")
)
)
which gives
[1] "c b a" "c a b" "b c a" "b a c" "a b c" "a c b" "e d" "d e"

How to order an array of character according to the numbers that it contains

I would like to order the following vector of chr:
x=c("class 1", "class 2", "class 4", "class 7", "class 5", "class 3", "class 6",
"class 10", "class 9", "class 11", "class 8", "class 12", "class 21")
according to the numbers that appear in the characters. E.g., in this case, the desired result is:
class 1, class 2, class 3, class 4, class 5, class 6, class 7, class 8, class 9, class 10, class 11
class 12, class 21
I tried with:
x[order(x)]
but obtaining a different result:
> x[order(x)]
[1] "class 1" "class 10" "class 11" "class 12" "class 2" "class 21" "class 3"
[8] "class 4" "class 5" "class 6" "class 7" "class 8" "class 9"
As mentioned, it is sorting alphabetically, and not considering the numeric value contained within the string.
There are a number of options to address this:
library(stringr)
str_sort(x, numeric = TRUE)
[1] "class 1" "class 2" "class 3" "class 4" "class 5" "class 6" "class 7" "class 8" "class 9" "class 10" "class 11" "class 12" "class 21"
Or
library(gtools)
mixedsort(x)
[1] "class 1" "class 2" "class 3" "class 4" "class 5" "class 6" "class 7" "class 8" "class 9" "class 10" "class 11" "class 12" "class 21"
Or without using another package, strip away "class" and use the numeric result to sort:
values <- as.numeric(gsub("class", "", x))
x[order(values)]
[1] "class 1" "class 2" "class 3" "class 4" "class 5" "class 6" "class 7" "class 8" "class 9" "class 10" "class 11" "class 12" "class 21"
That's because x is a vector of class "character" and elements (strings) are ordered alphabetically. Extract number from the strings an convert them to numeric type
y <- as.integer(substr(x, 7,8))
# y has the same order that x
# sort integers (numeric order) and match positions of unordered intergers
# match returns indexes of y ordered by sort(y)
x[match(y, sort(y))]
# Output is:
# [1] "class 1" "class 2" "class 7" "class 6" "class 5" "class 4" "class 3" "class 11" "class 9" "class 8" "class 10" "class 12"
# [13] "class 21"

Convert a dataframe to large character

I have a dataframe but need to convert it to a large character. Here is an example of the dataframe structure:
texts <- c("TEXT 1", "TEXT 2", "TEXT 3")
data <- data.frame(texts)
I need this structure:
[1] "TEXT 1" "TEXT 2" "TEXT 3"
I already tried using function as.character() , but it does not work as it converts all the lines to a single line.
You can transpose and concatenate, i.e.
c(t(data))
#[1] "TEXT 1" "TEXT 2" "TEXT 3"

concatenate two lists of string in r

Here is my sample:
a = c("a","b","c")
b = c("1","2","3")
I need to concatenate a and b automatically. The result should be "a 1","a 2","a 3","b 1","b 2","b 3","c 1","c 2","c 3".
For now, I am using the paste function:
paste(a[1],b[1])
I need an automatic way to do this. Besides writing a loop, is there any easier way to achieve this?
c(outer(a, b, paste))
# [1] "a 1" "b 1" "c 1" "a 2" "b 2" "c 2" "a 3" "b 3" "c 3"
Other options are :
paste(rep.int(a,length(b)),b)
or :
with(expand.grid(b,a),paste(Var2,Var1))
You can do:
c(sapply(a, function(x) {paste(x,b)}))
[1] "a 1" "a 2" "a 3" "b 1" "b 2" "b 3" "c 1" "c 2" "c 3"
edited paste0 into paste to match OP update

Resources