1

I'd like to create a vector/list containing a series of variables that are the result of the combination of two vectors containing (i) specific variable names and (ii) specific variable ID (same for all the variables). Here are reported a short version of the two vectors: the variable names:

names<-c("XPTS", "TROCK", "JFSG")

and the variable IDs:

values<-c(1, 1.1, 1.2, 1.3, 2, 2.1, 2.2, 2.3, 3, 3.1, 3.2, 3.3, 4, 4.1, 4.2, 4.3, 5, 5.1, 5.2, 5.3, 6, 6.1, 6.2, 6.3, 7, 7.1, 7.2, 7.3, 8, 8.1, 8.2, 8.3, 9, 9.1, 9.2, 9.3, 10, 10.1, 10.2, 10.3, 11, 11.1, 11.2, 11.3, 12, 12.1, 12.2, 12.3, 13, 13.1, 13.2, 13.3, 14, 14.1, 14.2, 14.3, 15, 15.1, 15.2, 15.3, 16, 16.1, 16.2, 16.3, 17, 17.1, 17.2, 17.3, 18, 18.1, 18.2, 18.3, 19, 19.1, 19.2, 19.3, 20, 20.1, 20.2, 20.3, 21, 21.1, 21.2, 21.3, 22, 22.1, 22.2, 22.3, 23, 23.1, 23.2, 23.3, 24, 24.1, 24.2, 24.3, 25, 25.1, 25.2, 25.3, 26, 26.1, 26.2, 26.3, 27, 27.1, 27.2, 27.3, 28, 28.1, 28.2, 28.3, 29, 29.1, 29.2, 29.3, 30, 30.1, 30.2, 30.3, 31, 31.1, 31.2, 31.3, 32, 32.1, 32.2, 32.3, 33, 33.1, 33.2, 33.3, 34, 34.1, 34.2, 34.3, 35, 35.1, 35.2, 35.3, 36, 36.1, 36.2, 36.3, 37, 37.1, 37.2, 37.3, 38, 38.1, 38.2, 38.3, 39, 39.1, 39.2, 39.3, 40, 40.1, 40.2, 40.3, 41, 41.1, 41.2, 41.3, 42, 42.1, 42.2, 42.3, 43, 43.1, 43.2, 43.3, 44, 44.1, 44.2, 44.3, 45, 45.1, 45.2, 45.3, 46, 46.1, 46.2, 46.3, 47, 47.1, 47.2, 47.3, 48, 48.1, 48.2, 48.3, 49, 49.1, 49.2, 49.3, 50)

I'd live to obtain a list of variable names as follows:

"XPTS_1","XPTS_1.1","XPTS_1.2", ..., "XPTS_49.3","XPTS_50","TROCK_1","TROCK_1.1",...,"TROCK_49.3","TROCK_50","JFSG_1","JFSG_1.1",...,"JFSG_49.3","JFSG_50"

The variable names are not only those reported but might change, so I'd like to have a dynamic loop for dealing with it. The one I wrote, as follows, doesn't fit my purpose:

variables_ID<-for (i in 1:length(values)) {
  paste(names, values[i], sep = "_")
}

since I get only

"XPTS_50" "TROCK_50" "JFSG_50"

1 Answers1

0

We can use outer

out1 <- c(t(outer(names, values, paste, sep="_")))

NOTE: transposed just to show that we get identical results with rep


Or use rep to replicate the 'names' and then paste

out2 <- paste(rep(names, each = length(values)), values, sep="_")
all.equal(out1, out2)
#[1] TRUE

head(out1)
#[1] "XPTS_1"   "XPTS_1.1" "XPTS_1.2" "XPTS_1.3" "XPTS_2"   "XPTS_2.1"
tail(out1)
#[1] "JFSG_48.3" "JFSG_49"   "JFSG_49.1" "JFSG_49.2" "JFSG_49.3" "JFSG_50"  

Or using CJ

library(data.table)
CJ(names, values)[, paste(names, values, sep="_")]

Or with tidyverse

library(tidyverse)
crossing(names, values) %>%
     unite(names, names, values) %>% 
     pull(names)
akrun
  • 874,273
  • 37
  • 540
  • 662
  • Thank you very much for the advice, it is very close to the result I need but it doesn't fulfil my goal properly since I'd need the variable in the order I have them in the vector "names". I'd actually need them saved as follows:XPTS_1","XPTS_1.1","XPTS_1.2", ..., "XPTS_49.3","XPTS_50","TROCK_1","TROCK_1.1",...,"TROCK_49.3","TROCK_50","JFSG_1","JFSG_1.1",...,"JFSG_49.3","JFSG_50", and not as the ouput of outer ("XPTS_1" "TROCK_1" "JFSG_1" "XPTS_1.1" "TROCK_1.1" "JFSG_1.1" etc.) – Eugenio Alladio Jul 02 '19 at 06:32
  • @EugenioAlladio Please check the output. I get in the same order – akrun Jul 02 '19 at 06:35