Dear all, I have a data frame that looks like this: c1 c2 c3 A B C B C A A A B and so on; I?d like to produce one single vector consisting of the columns c1,c2, c3, such that vector=("A","B","A","B","C","A","C","A","B") I guess it?s easy to do but I don?t know how...Can anyone help me? Thanks a lot! Christoph
Dear Christoph,> as.vector(as.matrix(DF))[1] "A" "B" "A" "B" "C" "A" "C" "A" "B" does the trick. Regards, John -------------------------------- John Fox Department of Sociology McMaster University Hamilton, Ontario Canada L8S 4M4 905-525-9140x23604 http://socserv.mcmaster.ca/jfox --------------------------------> -----Original Message----- > From: r-help-bounces at stat.math.ethz.ch > [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of > Christoph Scherber > Sent: Friday, January 07, 2005 9:47 AM > To: 'r-help at stat.math.ethz.ch' > Subject: [R] coercing columns > > Dear all, > > I have a data frame that looks like this: > > c1 c2 c3 > A B C > B C A > A A B > > and so on; > > I?d like to produce one single vector consisting of the > columns c1,c2, c3, such that > > vector=("A","B","A","B","C","A","C","A","B") > > I guess it?s easy to do but I don?t know how...Can anyone help me? > > Thanks a lot! > Christoph > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html
On Fri, 2005-01-07 at 15:47 +0100, Christoph Scherber wrote:> Dear all, > > I have a data frame that looks like this: > > c1 c2 c3 > A B C > B C A > A A B > > and so on; > > I?d like to produce one single vector consisting of the columns c1,c2, > c3, such that > > vector=("A","B","A","B","C","A","C","A","B") > > I guess it?s easy to do but I don?t know how...Can anyone help me? > > Thanks a lot! > Christoph> as.vector(as.matrix(df))[1] "A" "B" "A" "B" "C" "A" "C" "A" "B" If you review the help for as.matrix, it indicates: 'as.matrix' is a generic function. The method for data frames will convert any non-numeric/complex column into a character vector using 'format' and so return a character matrix, except that all-logical data frames will be coerced to a logical matrix. So using as.matrix() converts the 'df' columns into a character matrix, which is then converted into a vector using as.vector(). HTH, Marc Schwartz
if your dataframe is called foo, as.vector(foo) Quoting Christoph Scherber <Christoph.Scherber at uni-jena.de>:> Dear all, > > I have a data frame that looks like this: > > c1 c2 c3 > A B C > B C A > A A B > > and so on; > > I?d like to produce one single vector consisting of the columns c1,c2, > c3, such that > > vector=("A","B","A","B","C","A","C","A","B") > > I guess it?s easy to do but I don?t know how...Can anyone help me? > > Thanks a lot! > Christoph > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html >_________________________________________________ Tarmo Remmel B.E.S. M.Sc.F. (Ph.D. Candidate) GUESS Research Group - Department of Geography University of Toronto, Toronto, Ontario, CANADA Phone: 416.946.3058 http://eratos.erin.utoronto.ca/remmelt
Christoph Scherber wrote:> Dear all, > > I have a data frame that looks like this: > > c1 c2 c3 > A B C > B C A > A A B > > and so on; > > I?d like to produce one single vector consisting of the columns c1,c2, > c3, such that > > vector=("A","B","A","B","C","A","C","A","B") > > I guess it?s easy to do but I don?t know how...Can anyone help me? >Christoph, Assuming c1, c2, and c3 are stored as factor and `x' is your data.frame, then you can do: unlist(lapply(x, as.character)) Otherwise if c1, c2, and c3 are already character then you can skip the lapply part and just do unlist. --sundar
How about as.vector(x) where x is the data? BTW a matrix in R is stored as a vector anyways (IIRC). -- robert.kruus at utoronto.ca You will feel hungry again in another hour. -------- It is rumored that on Fri, 07 Jan 2005 15:47:04 +0100 Christoph Scherber <Christoph.Scherber at uni-jena.de> wrote:> Dear all, > > I have a data frame that looks like this: > > c1 c2 c3 > A B C > B C A > A A B > > and so on; > > I?d like to produce one single vector consisting of the columns c1,c2, > c3, such that > > vector=("A","B","A","B","C","A","C","A","B") > > I guess it?s easy to do but I don?t know how...Can anyone help me? > > Thanks a lot! > Christoph > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html >--------
Coercion to matrix then to vector should work:> datV1 V2 V3 1 A B C 2 B C A 3 A A B> as.vector(as.matrix(dat))[1] "A" "B" "A" "B" "C" "A" "C" "A" "B" Andy> From: Christoph Scherber > > Dear all, > > I have a data frame that looks like this: > > c1 c2 c3 > A B C > B C A > A A B > > and so on; > > I?d like to produce one single vector consisting of the > columns c1,c2, > c3, such that > > vector=("A","B","A","B","C","A","C","A","B") > > I guess it?s easy to do but I don?t know how...Can anyone help me? > > Thanks a lot! > Christoph > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html > >