I have a problem in R and it is the following: How can you assign a value to an element and then later recall an element in who's name you refer to the previously defined element.
Thus you define an element x
i <- value
Later you use x.i where "i" should be its value.
This is a problem in the following two cases:
1) First you create 10 elements with the name x.1 till x.10
for(i in 1:10){
assign(paste0("X.", i), 1:3)
}
Then you want to change the name of the elements in x.1 till x.10
for(i in 1:10){
assign(names(paste0("X.", i)), c("foo","bar","norf"))
}
This does not work.
2) I want to define two values:
year <- 1
code <- 2
And then in a dataframe "Data.year" (="Data.1") only those observations where the colum "code" is equal to the value of the previously defined "code" (=2) should be stored. With the name format: "Data.year.code" (=Data.1.2)
assign(paste0("Data.", i, code, sep="."), as.name(paste("Data",year , sep="."))[as.name(paste("Data",y , sep="."))$code==code,])
Here I tried to use as.name function in but this does not work. The problem is that R can obviously not reconise that "year" and "code" in the expression "Data.year.code" have a value. In stata you solve this by using `, But I do not now how you do this in R. Normally I just google something when I do not know the answer. But I have no idea how I should name this problem and thus can't find it...
It should have an easy and straightforward solution.