[R] Join data frame columns
Rui Barradas
ruipbarradas at sapo.pt
Tue Oct 23 16:42:35 CEST 2012
Hello,
Inline.
Em 23-10-2012 15:23, Bert Gunter 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??):
>
>> dat <- data.frame(a = 1:3, b=letters[1:3])
>> dat
> a b
> 1 1 a
> 2 2 b
> 3 3 c
>> unlist(dat)
> a1 a2 a3 b1 b2 b3
> 1 2 3 1 2 3
>
> So, a guess would be that the OP wants:
>
>> unlist(lapply(dat,as.character))
> a1 a2 a3 b1 b2 b3
> "1" "2" "3" "a" "b" "c"
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
>
> 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:
>> 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.
>>
>> ______________________________________________
>> 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.
>
>
More information about the R-help
mailing list