Meredith Ballard LaBeau
2012-Sep-28 21:51 UTC
[R] Merging multiple columns into one column
Good Evening- I have a dataframe that has 10 columns that has a header and 7306 rows in each column, I want to combine these columns into one. I utilized the stack function but it only returned 3/4 of the data...my code is: where nfcuy_bw is the dataframe with 7305 obs. and 10 variables Once I apply this code I only receive a data frame with 58440 obs. of 2 variables, of which there should be 73,050 obs. of 2 variables, just wondering what is happening here? View(nfcuy_bw) attach(nfcuy_bw) cuyahoga_nf<-data.frame(s5,s10,s25,s27,s33,s41,s51,his_c) cuy_nf<-stack(cuyahoga_nf) Thanks Meredith -- Doctoral Candidate Department of Civil and Environmental Engineering Michigan Technological University [[alternative HTML version deleted]]
On Sep 28, 2012, at 2:51 PM, Meredith Ballard LaBeau wrote:> Good Evening- > I have a dataframe that has 10 columns that has a header and 7306 rows in > each column, I want to combine these columns into one. I utilized the stack > function but it only returned 3/4 of the data...my code is: > where nfcuy_bw is the dataframe with 7305 obs. and 10 variables > Once I apply this code I only receive a data frame with 58440 obs. of 2 > variables, of which there should be 73,050 obs. of 2 variables, just > wondering what is happening here? > > View(nfcuy_bw) > > attach(nfcuy_bw)Using 'attach' is a great way to produce confusing errors.> > cuyahoga_nf<-data.frame(s5,s10,s25,s27,s33,s41,s51,his_c) > > cuy_nf<-stack(cuyahoga_nf)Unable to do much else in the absence of a dataset, much less a summary of these objects, .... whose creation is your responsibility, not ours. -- David Winsemius, MD Alameda, CA, USA
?unlist (A data frame is a list, as ?data.frame explains. Also the Intro to R tutorial, which should be read by everyone beginning with R). -- Bert On Fri, Sep 28, 2012 at 2:51 PM, Meredith Ballard LaBeau <mmballar at mtu.edu> wrote:> Good Evening- > I have a dataframe that has 10 columns that has a header and 7306 rows in > each column, I want to combine these columns into one. I utilized the stack > function but it only returned 3/4 of the data...my code is: > where nfcuy_bw is the dataframe with 7305 obs. and 10 variables > Once I apply this code I only receive a data frame with 58440 obs. of 2 > variables, of which there should be 73,050 obs. of 2 variables, just > wondering what is happening here? > > View(nfcuy_bw) > > attach(nfcuy_bw) > > cuyahoga_nf<-data.frame(s5,s10,s25,s27,s33,s41,s51,his_c) > > cuy_nf<-stack(cuyahoga_nf) > > Thanks > Meredith > > -- > Doctoral Candidate > Department of Civil and Environmental Engineering > Michigan Technological University > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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.-- Bert Gunter Genentech Nonclinical Biostatistics Internal Contact Info: Phone: 467-7374 Website: http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm
HI, You can try melt().? Not sure how it will perform in large datasets. library(reshape) n<-1e3 ?set.seed(1) ?mat1<-matrix(rnorm(n,15),ncol=20,nrow=50) dat1<-data.frame(mat1) dat2<-data.frame(value=melt(dat1)[,2]) dim(dat2) #[1] 1000??? 1 head(dat2,6) #???? value #1 14.37355 #2 15.18364 #3 14.16437 #4 16.59528 #5 15.32951 #6 14.17953 A.K. ----- Original Message ----- From: Meredith Ballard LaBeau <mmballar at mtu.edu> To: r-help at r-project.org Cc: Sent: Friday, September 28, 2012 5:51 PM Subject: [R] Merging multiple columns into one column Good Evening- I have a dataframe that has 10 columns that has a header and 7306 rows in each column, I want to combine these columns into one. I utilized the stack function but it only returned 3/4 of the data...my code is: where nfcuy_bw is the dataframe with 7305 obs. and 10 variables Once I apply this code I only receive a data frame with 58440 obs. of 2 variables, of which there should be 73,050 obs. of 2 variables, just wondering what is happening here? View(nfcuy_bw) attach(nfcuy_bw) cuyahoga_nf<-data.frame(s5,s10,s25,s27,s33,s41,s51,his_c) cuy_nf<-stack(cuyahoga_nf) Thanks Meredith -- Doctoral Candidate Department of Civil and Environmental Engineering Michigan Technological University ??? [[alternative HTML version deleted]] ______________________________________________ R-help at 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.
Hi Meredith, Why not just use paste()? cuyahoga_nf$newcolumn <- paste(cuyahoga_nf[,1], cuyahoga_nf[,2], cuyahoga_nf[,3], ..... , cuyahoga_nf[,4]) Best, Steve Politzer_ahles ----- Original Message -----> From: Meredith Ballard LaBeau <mmballar@mtu.edu> > To: r-help@r-project.org > Cc: > Sent: Friday, September 28, 2012 5:51 PM > Subject: [R] Merging multiple columns into one column > > Good Evening- > I have a dataframe that has 10 columns that has a header and 7306 rows in > each column, I want to combine these columns into one. I utilized the stack > function but it only returned 3/4 of the data...my code is: > where nfcuy_bw is the dataframe with 7305 obs. and 10 variables > Once I apply this code I only receive a data frame with 58440 obs. of 2 > variables, of which there should be 73,050 obs. of 2 variables, just > wondering what is happening here? > > View(nfcuy_bw) > > attach(nfcuy_bw) > > cuyahoga_nf<-data.frame(s5,s10,s25,s27,s33,s41,s51,his_c) > > cuy_nf<-stack(cuyahoga_nf) > > Thanks > Meredith > > -- > Doctoral Candidate > Department of Civil and Environmental Engineering > Michigan Technological University > > ??? [[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]]