Hi, I have a data frame with 100 variables (numeric and non numeric types), and I want to join them in only one column, like a vector, but i want to keep the non numeric variables like they are. I know that i can do something like this: Suppose that my data is in df variable new_df<-data.frame(c(df[,1],df[,2],df[,3],df[,4],...........) This works but i have 100 variables! Any way of doing this a little bit faster? Thanks a lot! -- View this message in context: http://r.789695.n4.nabble.com/Join-data-frame-columns-tp4647113.html Sent from the R help mailing list archive at Nabble.com.
Hi! Either I don't understand what you want to do, or it doesn't make any sense. First, a vector cannot have different modes. If you want a single vector it will most likely be coerced in to characters; probably not what you want. Second, what you do is build a data.frame with another data.frame without doing anything. What is df? Provide the output of dput(df). How do you want new_df to look like? What is the difference with df? Ivan -- Ivan CALANDRA Universit? de Bourgogne UMR CNRS/uB 6282 Biog?osciences 6 Boulevard Gabriel 21000 Dijon, FRANCE +33(0)3.80.39.63.06 ivan.calandra at u-bourgogne.fr http://biogeosciences.u-bourgogne.fr/calandra Le 23/10/12 13:45, brunosm a ?crit :> Hi, > > I have a data frame with 100 variables (numeric and non numeric types), and > I want to join them in only one column, like a vector, but i want to keep > the non numeric variables like they are. > > I know that i can do something like this: > > Suppose that my data is in df variable > > new_df<-data.frame(c(df[,1],df[,2],df[,3],df[,4],...........) > > This works but i have 100 variables! > > Any way of doing this a little bit faster? > > Thanks a lot! > > > > > > -- > View this message in context: http://r.789695.n4.nabble.com/Join-data-frame-columns-tp4647113.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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 Ivan, thanks for your help! For example:> dfnum letters 1 1 A 2 2 B 3 3 C 4 4 D 5 5 E What i want is to join "num" and "letters" in a single column. Something like this> new_df1 1 2 2 3 3 4 4 5 5 6 A 7 B 8 C 9 D 10 E I tried to do it with a list but it converts factors to numbers...> new_df<-list(c(df[,1],df[,2])) > new_df[[1]] [1] 1 2 3 4 5 1 2 3 4 5 Thanks! -- View this message in context: http://r.789695.n4.nabble.com/Join-data-frame-columns-tp4647113p4647132.html Sent from the R help mailing list archive at Nabble.com.
Hi, Try this: set.seed(1) df1<-data.frame(col1=rnorm(10,15),col2=rep(c("a","b"),5),col3=sample(1:50,10,replace=TRUE),col4=sample(LETTERS[1:10],10,replace=TRUE)) df2<-do.call(rbind,lapply(df1,function(x) data.frame(x))) str(df2) 'data.frame':??? 40 obs. of? 1 variable: ?$ x: chr? "14.3735461892577" "15.1836433242221" "14.16437138759" "16.5952808021378" ... tail(df2) #??????? x #col4.5? I #col4.6? G #col4.7? H #col4.8? B #col4.9? H #col4.10 E ?head(df2) #????????????????????? x #col1.1 14.3735461892577 #col1.2 15.1836433242221 #col1.3?? 14.16437138759 #col1.4 16.5952808021378 #col1.5 15.3295077718154 #col1.6? 14.179531615882 A.K. ----- Original Message ----- From: brunosm <brunosm87 at gmail.com> To: r-help at r-project.org Cc: Sent: Tuesday, October 23, 2012 7:45 AM Subject: [R] Join data frame columns Hi, I have a data frame with 100 variables (numeric and non numeric types), and I want to join them in only one column, like a vector, but i want to keep the non numeric variables like they are. I know that i can do something like this: Suppose that my data is in df variable new_df<-data.frame(c(df[,1],df[,2],df[,3],df[,4],...........) This works but i have 100 variables! Any way of doing this a little bit faster? Thanks a lot! -- View this message in context: http://r.789695.n4.nabble.com/Join-data-frame-columns-tp4647113.html Sent from the R help mailing list archive at Nabble.com. ______________________________________________ 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.
Hello, Use apply/paste. apply(df, 1, paste, collapse = "") # outputs a vector Also, df is the name of an R function, you should choose something else, it can become confusing. Hope this helps, Rui Barradas Em 23-10-2012 12:45, brunosm escreveu:> Hi, > > I have a data frame with 100 variables (numeric and non numeric types), and > I want to join them in only one column, like a vector, but i want to keep > the non numeric variables like they are. > > I know that i can do something like this: > > Suppose that my data is in df variable > > new_df<-data.frame(c(df[,1],df[,2],df[,3],df[,4],...........) > > This works but i have 100 variables! > > Any way of doing this a little bit faster? > > Thanks a lot! > > > > > > -- > View this message in context: http://r.789695.n4.nabble.com/Join-data-frame-columns-tp4647113.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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.
Hello, Sorry for my earlier post, I misunderstood what you want. dat <- data.frame(letters, 1:26) as.vector(sapply(d, as.character)) Hope this helps, Rui Barradas Em 23-10-2012 14:13, brunosm escreveu:> Hi Ivan, thanks for your help! > > For example: > >> df > num letters > 1 1 A > 2 2 B > 3 3 C > 4 4 D > 5 5 E > > What i want is to join "num" and "letters" in a single column. Something > like this > >> new_df > 1 1 > 2 2 > 3 3 > 4 4 > 5 5 > 6 A > 7 B > 8 C > 9 D > 10 E > > I tried to do it with a list but it converts factors to numbers... > >> new_df<-list(c(df[,1],df[,2])) >> new_df > [[1]] > [1] 1 2 3 4 5 1 2 3 4 5 > > Thanks! > > > > -- > View this message in context: http://r.789695.n4.nabble.com/Join-data-frame-columns-tp4647113p4647132.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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 again, It seems that you want the modes to be conserved. This is just not possible; everything will be coerced to characters, as Rui highlighted. But the question is why do you want to do this? You would loose helpful information (i.e. modes). Maybe there is a better way to do what you want to do. HTH, Ivan -- Ivan CALANDRA Universit? de Bourgogne UMR CNRS/uB 6282 Biog?osciences 6 Boulevard Gabriel 21000 Dijon, FRANCE +33(0)3.80.39.63.06 ivan.calandra at u-bourgogne.fr http://biogeosciences.u-bourgogne.fr/calandra Le 23/10/12 16:15, Rui Barradas a ?crit :> Hello, > > Sorry for my earlier post, I misunderstood what you want. > > dat <- data.frame(letters, 1:26) > as.vector(sapply(d, as.character)) > > Hope this helps, > > Rui Barradas > Em 23-10-2012 14:13, brunosm escreveu: >> Hi Ivan, thanks for your help! >> >> For example: >> >>> df >> num letters >> 1 1 A >> 2 2 B >> 3 3 C >> 4 4 D >> 5 5 E >> >> What i want is to join "num" and "letters" in a single column. Something >> like this >> >>> new_df >> 1 1 >> 2 2 >> 3 3 >> 4 4 >> 5 5 >> 6 A >> 7 B >> 8 C >> 9 D >> 10 E >> >> I tried to do it with a list but it converts factors to numbers... >> >>> new_df<-list(c(df[,1],df[,2])) >>> new_df >> [[1]] >> [1] 1 2 3 4 5 1 2 3 4 5 >> >> Thanks! >> >> >> >> -- >> View this message in context: >> http://r.789695.n4.nabble.com/Join-data-frame-columns-tp4647113p4647132.html >> Sent from the R help mailing list archive at Nabble.com. >> >> ______________________________________________ >> 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. > > ______________________________________________ > 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. >