Skip to content

Join data frame columns

9 messages · Ivan Calandra, brunosm, arun +2 more

#
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 Ivan, thanks for your help!

For example:
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
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...
[[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:
#
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:
#
I don't think so. The OP wanted the list by columns, not rows, if I
understand correctly.

unlist(df)

provides this. Of course, the request probably does not makes sense in
the first place, as the different types/classes in the data.frame will
be coerced to one type/class. Moreover, factors will become numeric
(is this what motivated the original request??):
a b
1 1 a
2 2 b
3 3 c
a1 a2 a3 b1 b2 b3
 1  2  3  1  2  3

So, a guess would be that the OP wants:
a1  a2  a3  b1  b2  b3
"1" "2" "3" "a" "b" "c"

But without the requested reproducible example, this is only a guess.
Again, note the coercion of numerics to character.

I would also guess that the OP might benefit by reading the Intro to R
tutorial and the data frame section of the R Language Definition
Manual -- and perhaps ?data.frame -- to better understand the nature
of data frames and what can and cannot be done with them.

Of course, I may have missed the mark altogether, in which case, please ignore.

-- Bert
On Tue, Oct 23, 2012 at 6:48 AM, Rui Barradas <ruipbarradas at sapo.pt> wrote:

  
    
#
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,

Inline.
Em 23-10-2012 15:23, Bert Gunter escreveu:
Right. I've posted a similar solution, but I find this one better. In 
both of them, at least the coercion is explicit, so the user knows what 
to expect as output.

Rui Barradas