1

I am generating an input file for software. I ran into an inconsistency in paste0() function. Here are my example list of names.

vars <- c("1234_AS_SA1_PCNS","2345_AS_SA2_UDA", "3823_AS_SA3_CL")

I would like to print this:

 "Equal = (1234_AS_SA1_PCNS, Slope[0]),
          (2345_AS_SA2_UDA, Slope[0]),
          (3823_AS_SA3_CL, Slope[0]);"

I tried this but it did not do the job. SOmehow, "Equal" gets to the end.

paste0("Equal = ", 
       
       for(i in 1:length(vars)) {
         Equal <- paste0("(",vars[i], ", Slope[0])", ",")
         print(Equal)
       })


[1] "(1234_AS_SA1_PCNS, Slope[0]),"
[1] "(2345_AS_SA2_UDA, Slope[0]),"
[1] "(3823_AS_SA3_CL, Slope[0]),"
[1] "Equal = "

This function does not employ ","s correctly as well as a ";" at the end.

Any thoughts? Thanks.

2
  • 1
    Do you need cat(paste0("Equal = ", paste("(", vars, ", Slope[0])", collapse=", ", sep=""))) Commented Jun 25, 2020 at 21:33
  • 1
    I hope you aren't trying to generate R code by mainpulating text. Commented Jun 25, 2020 at 21:33

2 Answers 2

1

We could do this without a loop as paste is vectorized

cat(paste0("Equal = ", paste("(", vars, ", Slope[0])",
        collapse=",\n ", sep=""), ";"))
Equal = (1234_AS_SA1_PCNS, Slope[0]),
 (2345_AS_SA2_UDA, Slope[0]),
 (3823_AS_SA3_CL, Slope[0]);

If we need to have double quotes at the end

cat(dQuote(paste0("Equal = ", paste("(", vars, ", Slope[0])",
        collapse=",\n ", sep=""), ";"), FALSE))
"Equal = (1234_AS_SA1_PCNS, Slope[0]),
 (2345_AS_SA2_UDA, Slope[0]),
 (3823_AS_SA3_CL, Slope[0]);"

Or may be

cat(dQuote(paste0("Equal = ", paste(sprintf('"(%s, Slope[0])"', vars), collapse=",\n ", sep="")), FALSE))
"Equal = "(1234_AS_SA1_PCNS, Slope[0])",
 "(2345_AS_SA2_UDA, Slope[0])",
 "(3823_AS_SA3_CL, Slope[0])""
Sign up to request clarification or add additional context in comments.

12 Comments

Oh actually, I wanted to have quotes (") beginning and the end. Is that a minor edit in your code?
@amisos55 Yes, you can wrap with dQuote(paste0(....), FALSE)
Thanks that works just fine. May I ask one last thing? "Equal = (1234_AS_SA1_PCNS, Slope[0])", "(2345_AS_SA2_UDA, Slope[0])", "(3823_AS_SA3_CL, Slope[0]);" is quoting every element possible? I apologize that this was not what I asked in the original question.
@amisos55 In that case, you can use dQuote on the dQuote(vars, FALSE) within the paste
@amisos55 updaed the post
|
1

You can't use a for loop to "expand" into function arguments like that. Especially not by trying to print them. This isn't Bash.

vars <- c("1234_AS_SA1_PCNS","2345_AS_SA2_UDA", "3823_AS_SA3_CL")

# Generate each "(<var>, Slope[0])" section
sections <- as.list(paste0("(", vars, ", Slope[0])"))

# Join the sections with ", "
sections_str <- do.call(paste, c(sections, sep = ", "))

# Construct the final string
result <- paste0("Equal = ", part2)

print(result)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.