1

The intention of my code is to concatenate the values from the vector v. To do this, I created a function concat with two arguments vector, SID. But for reasons I don't understand,

#A character vector to which other strings will be appended
    
v <- c("R_2wmKOSbPWHl4VtT2","R_2TtslLEVNeHs2r73","R_ZF79IJ60LaxxsuR4","R_3JJDUkrZ07eIwnh5","R_3JrWuv9fsLK6qNx6")

concat <- function(vector,SID){
  
  decrement_append <- "&decrementQuotas=true"
  SID_append <- "?surveyId="
  
  for(i in 1:length(vector)){
    out[i] <- paste0(v[i],SID_append,SID,decrement_append)
  }
  out[i]
}

And:

concat(vector = v,
       SID = "SV_55tYjKDRKYTRNIh")

When I run this, I get:

Error in concat(vector = v, SID = "SV_55tYjKDRKYTRNIh") : 
  object 'out' not found

I've tried it a couple of other ways, such as:

   concat <- function(vector,SID){
  
  decrement_append <- "&decrementQuotas=true"
  SID_append <- "?surveyId="
  
  new_vector <- for(i in 1:length(vector)){
   out[i] <- paste0(v[i],SID_append,SID,decrement_append)
  }
  new_vector
}

But I'm getting the same error.

Stephen Poole
  • 371
  • 3
  • 9

1 Answers1

2

The out is not initialized in the function

concat <- function(vector,SID){
   out <- character(length(vector))
  
   decrement_append <- "&decrementQuotas=true"
   SID_append <- "?surveyId="
  
   for(i in 1:length(vector)){
     out[i] <- paste0(v[i],SID_append,SID,decrement_append)
   }
   out
 }

-testing

> concat(v, "SV_55tYjKDRKYTRNIh")
[1] "R_2wmKOSbPWHl4VtT2?surveyId=SV_55tYjKDRKYTRNIh&decrementQuotas=true" "R_2TtslLEVNeHs2r73?surveyId=SV_55tYjKDRKYTRNIh&decrementQuotas=true"
[3] "R_ZF79IJ60LaxxsuR4?surveyId=SV_55tYjKDRKYTRNIh&decrementQuotas=true" "R_3JJDUkrZ07eIwnh5?surveyId=SV_55tYjKDRKYTRNIh&decrementQuotas=true"
[5] "R_3JrWuv9fsLK6qNx6?surveyId=SV_55tYjKDRKYTRNIh&decrementQuotas=true"

paste/paste0 are vectorized. So, looping is not really needed

concat2 <- function(vector,SID){
    
  
    decrement_append <- "&decrementQuotas=true"
    SID_append <- "?surveyId="
  
    
       paste0(v, SID_append,SID,decrement_append)
    

  }

-testing

> concat2(v, "SV_55tYjKDRKYTRNIh")
[1] "R_2wmKOSbPWHl4VtT2?surveyId=SV_55tYjKDRKYTRNIh&decrementQuotas=true" "R_2TtslLEVNeHs2r73?surveyId=SV_55tYjKDRKYTRNIh&decrementQuotas=true"
[3] "R_ZF79IJ60LaxxsuR4?surveyId=SV_55tYjKDRKYTRNIh&decrementQuotas=true" "R_3JJDUkrZ07eIwnh5?surveyId=SV_55tYjKDRKYTRNIh&decrementQuotas=true"
[5] "R_3JrWuv9fsLK6qNx6?surveyId=SV_55tYjKDRKYTRNIh&decrementQuotas=true"
akrun
  • 874,273
  • 37
  • 540
  • 662
  • Can you tell me how I test if a function is vectorized? – Stephen Poole Jan 03 '22 at 19:10
  • 1
    @StephenPoole Perhaps [this](https://stackoverflow.com/questions/58568392/how-do-i-know-a-function-or-an-operation-in-r-is-vectorized) helps – akrun Jan 03 '22 at 19:11
  • Thanks for that.. So if I'm understanding correctly, because my vector v is atomic, paste0 is vectorized. However, if I had a data frame, paste0 would not be vectorized. Is that right? – Stephen Poole Jan 03 '22 at 19:18
  • 1
    @StephenPoole Each function have a different requirement. Here `paste` requires a vector/vectors as argument. `... - one or more R objects, to be converted to character vectors.` you are passing an atomic/scalar to paste, which it can take, but it would be more efficient to do this on the whole vector – akrun Jan 03 '22 at 19:21
  • 1
    It it is a data.frame, you may have to loop over the columns with `lapply/sapply` if you need to paste each column separately – akrun Jan 03 '22 at 19:21