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\")"
- How to I modify the
forstatement so that\is not introduced before each". - 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.
cat("structField( \"mpg\" ,\"double\")")will give mestructField( "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.structType(structField("eruptions", "double"), structField("waiting", "double"),..i.e. in thestructTypefunction. Thanks