Hello,
Let's say I have the following two objects.
vars = "x,y,z"
total= "Total"
I want to create the following one:
"ID" "x" "y" "z"
To do so, I’m trying to separate the elements from “var” as follows:
mylist = as.list(strsplit(vars, ",")[[1]])
Then, I’m using the “paste” function
paste("ID",mylist[[1]],mylist[[2]], mylist[[3]],sep=",") #Here I tried with sep= \”
However, I have no been being able to get what I want. I tried many combinations, but I was not able to get each element within double quotes. Can you please advise?
Further information: In case you are wondering why I want this, I’ll explain. I want to create a function that perform certain analyses. The first step is to read the data set I want along, along with specific variables:
MyFunc = function(id,vars,total){
mylist = as.list(strsplit(vars, ",")[[1]])
dat = subset(mydat, select = paste("id",mylist[[1]],mylist[[2]], mylist[[3]],sep=","))
}
MyFunc(id = "ID",
vars = "x,y,z",
total = "Total")
Thanks a lot for your support.
A.G.
Note: I do not provide an example data set because I don’t think is necessary.