0

Suggest I have the following character vector:

    models      <-    c(CNRM_CERFACS_CNRM_CM5_ALADIN53_r1i1p1,
                        CNRM_CERFACS_CNRM_CM5_ALARO_0_r1i1p1,
                        CNRM_CERFACS_CNRM_CM5_CCLM4_8_17_r1i1p1,
                        CNRM_CERFACS_CNRM_CM5_RCA4_r1i1p1,
                        ICHEC_EC_EARTH_CCLM4_8_17_r12i1p1,
                        ICHEC_EC_EARTH_HIRHAM5_r3i1p1,
                        ICHEC_EC_EARTH_RACMO22E_r1i1p1,
                        ICHEC_EC_EARTH_RCA4_r12i1p1,
                        IPSL_IPSL_CM5A_MR_RCA4_r1i1p1,
                        IPSL_IPSL_CM5A_MR_WRF331F_r1i1p1,
                        MPI_M_MPI_ESM_LR_CCLM4_8_17_r1i1p1,
                        MPI_M_MPI_ESM_LR_RCA4_r1i1p1,
                        MPI_M_MPI_ESM_LR_REMO2009_r1i1p1,
                        MPI_M_MPI_ESM_LR_REMO2009_r2i1p1
    )

I now want to convert these 14 character objects into strings, i.e. add quotation marks at the beginning and ending of each of these names to get this

models <-    ("CNRM_CERFACS_CNRM_CM5_ALADIN53_r1i1p1",
              "CNRM_CERFACS_CNRM_CM5_ALARO_0_r1i1p1",... 

Is there a form of doing that automatically, avoiding doing by hand?

Mike
  • 21
  • 4
  • Those would already have quotes around them if that was a character vector. – Cybernetic Mar 23 '18 at 20:20
  • There is no such thing as a "string vector" in R, strings aren't vector types in base R. `char` is what you're looking for, but `models` is not necessarily a char vector, it is a vector of whatever type the variables you passed into `c()` are. – Alec K Mar 23 '18 at 20:33

2 Answers2

0

c() erase the names of the variables you concatenate. You should use an object that keeps the names and then access it I guess. Where do these names come from? I am sure there is no need to write them all explicitely...

For instance this does what you want...

models      <-    data.frame(CNRM_CERFACS_CNRM_CM5_ALADIN53_r1i1p1,
                        CNRM_CERFACS_CNRM_CM5_ALARO_0_r1i1p1,
                        CNRM_CERFACS_CNRM_CM5_CCLM4_8_17_r1i1p1,
                        CNRM_CERFACS_CNRM_CM5_RCA4_r1i1p1,
                        ICHEC_EC_EARTH_CCLM4_8_17_r12i1p1,
                        ICHEC_EC_EARTH_HIRHAM5_r3i1p1,
                        ICHEC_EC_EARTH_RACMO22E_r1i1p1,
                        ICHEC_EC_EARTH_RCA4_r12i1p1,
                        IPSL_IPSL_CM5A_MR_RCA4_r1i1p1,
                        IPSL_IPSL_CM5A_MR_WRF331F_r1i1p1,
                        MPI_M_MPI_ESM_LR_CCLM4_8_17_r1i1p1,
                        MPI_M_MPI_ESM_LR_RCA4_r1i1p1,
                        MPI_M_MPI_ESM_LR_REMO2009_r1i1p1,
                        MPI_M_MPI_ESM_LR_REMO2009_r2i1p1
    )

colnames(models)
Frostic
  • 680
  • 4
  • 11
0

A work-around would be

models      <-    "CNRM_CERFACS_CNRM_CM5_ALADIN53_r1i1p1,
                    CNRM_CERFACS_CNRM_CM5_ALARO_0_r1i1p1,
                    CNRM_CERFACS_CNRM_CM5_CCLM4_8_17_r1i1p1,
                    CNRM_CERFACS_CNRM_CM5_RCA4_r1i1p1,
                    ICHEC_EC_EARTH_CCLM4_8_17_r12i1p1,
                    ICHEC_EC_EARTH_HIRHAM5_r3i1p1,
                    ICHEC_EC_EARTH_RACMO22E_r1i1p1,
                    ICHEC_EC_EARTH_RCA4_r12i1p1,
                    IPSL_IPSL_CM5A_MR_RCA4_r1i1p1,
                    IPSL_IPSL_CM5A_MR_WRF331F_r1i1p1,
                    MPI_M_MPI_ESM_LR_CCLM4_8_17_r1i1p1,
                    MPI_M_MPI_ESM_LR_RCA4_r1i1p1,
                    MPI_M_MPI_ESM_LR_REMO2009_r1i1p1,
                    MPI_M_MPI_ESM_LR_REMO2009_r2i1p1"
unlist(stsplit(models, split = ",\n"))
smanski
  • 541
  • 2
  • 7