Samir K.C.
2010-Apr-11 09:33 UTC
[R] How to assign variable name (of a vector, matrix, array) from element of another vector or by using paste?
Dear All, My problem is the following> varname <- c("age","sex","edu","mrt") > varstates <- c(10,2,4,3) # 10 age groups, 2 sexes, 4 education group and 3marital status.. Now, I want four variables (vector with length1) named nrage, nrsex, nredu, nrmrt and assing some values> for (i in 1:4) paste("nr",varname[i],sep="") <- varstates[i] #this doesnot work I want the following>nrage10>nrsex2 .. etc Thanks in advance Best Regards Samir K.C. [[alternative HTML version deleted]]
Jorge Ivan Velez
2010-Apr-11 14:20 UTC
[R] How to assign variable name (of a vector, matrix, array) from element of another vector or by using paste?
Hi Samir, Try this: varname <- c("age","sex","edu","mrt") varstates <- c(10,2,4,3) for(i in 1:4) { nam <- paste("nr", varname[i], sep="") assign(nam, varstates[i]) } nrage nrsex nredu nrmrt See ?assign for more information. HTH, Jorge On Sun, Apr 11, 2010 at 5:33 AM, Samir K.C. <> wrote:> Dear All, > My problem is the following > > > varname <- c("age","sex","edu","mrt") > > varstates <- c(10,2,4,3) # 10 age groups, 2 sexes, 4 education group and > 3 > marital status.. > Now, I want four variables (vector with length1) named nrage, nrsex, nredu, > nrmrt and assing some values > > > for (i in 1:4) paste("nr",varname[i],sep="") <- varstates[i] #this does > not work > > > I want the following > >nrage > 10 > >nrsex > 2 > .. etc > > Thanks in advance > Best Regards > Samir K.C. > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]