About the first step (not that something is wrong with what you are doing), you can create the names like this:
temp <- combn(xvec, 2, FUN=paste, collapse=".")
This makes all combinations and then using paste
it collapses the combinations together. I use .
, because *
is not very nice in a variable name. You can also check ?make.names
, a function that makes strings suitable for use as names.
Second step
You can use assign
to create variables from a string stored in variable. (get
is when you have the name of an existing variable as a string and want to access it)
Try something like:
for(nm in make.names(temp)) {
assign(nm, "Put something more interesting here")
}
You can see all objects in your environment using ls()
ls()
## [1] "age.edu" "age.part" "edu.part"
## [4] "inftype.age" "inftype.edu" "inftype.part"
## [7] "inftype.usecondom" "married.age" "married.edu"
## [10] "married.inftype" "married.part" "married.usecondom"
## [13] "nm" "temp" "usecondom.age"
## [16] "usecondom.edu" "usecondom.part" "white.age"
## [19] "white.edu" "white.inftype" "white.married"
## [22] "white.part" "white.usecondom" "xvec"
Now you have created a lot of variables.
Just as a comment, I wanted to add how I would probably do it.
Instead of filling your environment with a lot of objects you can use a list (myCombs
) to save all of them.
myCombs <- combn(xvec, 2,simplify=FALSE, FUN = function(cmb) {
res <- paste("This is the combination of", cmb[1], "and", cmb[2])
res
})
##Add the names to the list items.
names(myCombs) <- combn(xvec, 2, FUN=paste, collapse=".")
I used the terms to construct a string. You probably want to do something more sophisticated. If you have the items of xvec
as variables in your environment you can access them here using get(cmb[1])
and get(cmb[1])
.
Now you can access each variable using myCombs$NAME
, or myComb[[NAME]]
, or you can even attach(myComb)
the whole list to your environment.
myCombs$edu.part
## [1] "This is the combination of edu and part"
I started to write a small answer, but got carried away. Hope this helps you,
alex