2

With names(mtcars) we get:

 [1] "mpg"  "cyl"  "disp" "hp"   "drat" "wt"   "qsec" "vs"   "am"   "gear"
[11] "carb"

I want to programmaically construct a string to pass to as an argument to another function, this is what I want it to look like: structField("mpg", "double"), structField("cyl", "double"),structField("disp", "double")...


With what I know, I could come up with this: a = vector("character", ncol(mtcars))

Create a vector a and give it names from mtcars and use the names in for loop

names(a) = names(mtcars)  

for(i in names(mtcars)) a[i] = unquote(paste0("structField","(", '"',i,'"', ",", '"', "double", '"', ")"))    

This last for statement creates the following vector: mpg cyl "structField(\"mpg\",\"double\")" "structField(\"cyl\",\"double\")" disp hp "structField(\"disp\",\"double\")" "structField(\"hp\",\"double\")" drat wt "structField(\"drat\",\"double\")" "structField(\"wt\",\"double\")" qsec vs "structField(\"qsec\",\"double\")" "structField(\"vs\",\"double\")" am gear "structField(\"am\",\"double\")" "structField(\"gear\",\"double\")" carb "structField(\"carb\",\"double\")"


  1. How to I modify the for statement so that \ is not introduced before each ".
  2. My idea is to use paste(a, collapse = ",") to get the final string that looks like: "structField(\"mpg\",\"double\"),structField(\"cyl\",\"double\"),structField(\"disp\",\"double\"),..., albeit without the \, but is there a more efficient to do this?

Thanks.

3
  • OK I see that in R cat("structField( \"mpg\" ,\"double\")") will give me structField( "mpg" ,"double") so that is useful in getting the final string I am interested in, still I am open to learning how to do this better. Commented Dec 9, 2017 at 7:10
  • This doesn't seem like something that would be an optimal. What is your goal down the line? Commented Dec 9, 2017 at 7:10
  • @RomanLuštrik I am using it as such structType(structField("eruptions", "double"), structField("waiting", "double"),.. i.e. in the structType function. Thanks Commented Dec 9, 2017 at 7:13

1 Answer 1

1

Use this:

sapply(names(mtcars), function(x)paste0("structField(","'",x,"'",",","'" ,typeof(mtcars[[x]]),"'",")"))

Output:

> sapply(names(mtcars), function(x)paste0("structField(","'",x,"'",",","'" ,typeof(mtcars[[x]]),"'",")"))
                           mpg                            cyl 
 "structField('mpg','double')"  "structField('cyl','double')" 
                          disp                             hp 
"structField('disp','double')"   "structField('hp','double')" 
                          drat                             wt 
"structField('drat','double')"   "structField('wt','double')" 
                          qsec                             vs 
"structField('qsec','double')"   "structField('vs','double')" 
                            am                           gear 
  "structField('am','double')" "structField('gear','double')" 
                          carb 
"structField('carb','double')" 
Sign up to request clarification or add additional context in comments.

4 Comments

I see that you are purposing switching the " with '. So otherwise I wouldn't be able to use " is that so?
@B.L.Sher, As per my understanding yes, The problem is if you start a string with a "(quote), then it encounters a quote("), now R knows that you don't want to end the string over there hence it puts an escape charter to avoid the confusion internally and at the same time tries to segregate the strings which are present within same quotes. To circumvent the problem you can use mixture of double and single quote. () in programming languages often called as escape character and its story comes from UNIX. Because of this escape property when you print it , it is simply give you a normal string.
Thanks for you help!
@B.L.Sher, Welcome, please upvote incase if you liked the solution

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.