I would like to create a series of vectors in a loop and name them with letters. I know the command letters gives me the 26 letters of the alphabet. For example, I have a dataframe, called A, with 6 columns.>B<-length(A) >C<-letters(1:B)[1] "a" "b" "c" "d" "e" "f" I would like to extract the first letter "a", and put the first column of dataframe A in a vector called "a">C<-e[1][1] "a" but when I type>e[1]=A[,1]Warning message: In e[1] =A[, 1] : number of items to replace is not a multiple of replacement length It thinks I am trying to replace the first element of e with the first column of A. is there any other way in which I can achieve this? Thanks in advance -- View this message in context: http://www.nabble.com/naming-vectors--matrices-in-R-tp22802121p22802121.html Sent from the R help mailing list archive at Nabble.com.
emj83 <stp08emj <at> shef.ac.uk> writes:> > > I would like to create a series of vectors in a loop and name them with > letters. > > I know the command letters gives me the 26 letters of the alphabet. > > For example, I have a dataframe, called A, with 6 columns. > > >B<-length(A) > >C<-letters(1:B) > [1] "a" "b" "c" "d" "e" "f" > > I would like to extract the first letter "a", and put the first column of > dataframe A in a vector called "a" > > >C<-e[1] > [1] "a" > > but when I type > > >e[1]=A[,1] > > Warning message: > In e[1] =A[, 1] : > number of items to replace is not a multiple of replacement length >This is really difficult to understand. Try to make the example so that is can be pasted in R. For example, where is your dataframe A? A = data.frame(a=1:10,b=1:10) e = A[,1] e Dieter
This is probably what you want mydf<-as.data.frame(matrix(1:60,10)) for (i in (1:dim(mydf)[2])) assign(letters[i],mydf[,i]) I would not, however, recommend it. It really does not produce very readable code. emj83 wrote:> I would like to create a series of vectors in a loop and name them with > letters. > > I know the command letters gives me the 26 letters of the alphabet. > > For example, I have a dataframe, called A, with 6 columns. > >> B<-length(A) >> C<-letters(1:B) > [1] "a" "b" "c" "d" "e" "f" > > I would like to extract the first letter "a", and put the first column of > dataframe A in a vector called "a" > >> C<-e[1] > [1] "a" > > but when I type > >> e[1]=A[,1] > > Warning message: > In e[1] =A[, 1] : > number of items to replace is not a multiple of replacement length > > It thinks I am trying to replace the first element of e with the first > column of A. > > is there any other way in which I can achieve this? > > Thanks in advance > > >-- Erich Neuwirth, University of Vienna Faculty of Computer Science Computer Supported Didactics Working Group Visit our SunSITE at http://sunsite.univie.ac.at Phone: +43-1-4277-39464 Fax: +43-1-4277-39459