[R] combining columns into a "combination index" of the same length

Thierry Onkelinx thierry.onkelinx at inbo.be
Tue Jul 21 16:00:32 CEST 2015


Please always keep the mailing list in cc.

If mat is a data.frame, then you can use do.call. Then the number of
columns doesn't matter.

do.call(paste, mtcars[, c("mpg", "cyl")])
do.call(paste, mtcars[, c("mpg", "cyl", "disp")])
do.call(paste, mtcars)


ir. Thierry Onkelinx
Instituut voor natuur- en bosonderzoek / Research Institute for Nature and
Forest
team Biometrie & Kwaliteitszorg / team Biometrics & Quality Assurance
Kliniekstraat 25
1070 Anderlecht
Belgium

To call in the statistician after the experiment is done may be no more
than asking him to perform a post-mortem examination: he may be able to say
what the experiment died of. ~ Sir Ronald Aylmer Fisher
The plural of anecdote is not data. ~ Roger Brinner
The combination of some data and an aching desire for an answer does not
ensure that a reasonable answer can be extracted from a given body of data.
~ John Tukey

2015-07-21 15:43 GMT+02:00 Emmanuel Levy <emmanuel.levy op gmail.com>:

> Thanks! -- this is indeed much faster (plus I made a mistake, one has to
> use paste with the option collapse="".
>
> The thing is I'm looking for a solution *without paste*. The reason is
> that* there may be two or more columns*.
>
>
>
> On 21 July 2015 at 16:32, Thierry Onkelinx <thierry.onkelinx op inbo.be>
> wrote:
>
>> Yes. paste0() can work on vectors. So paste0(mat[, col1], mat[, col2])
>>
>> ir. Thierry Onkelinx
>> Instituut voor natuur- en bosonderzoek / Research Institute for Nature
>> and Forest
>> team Biometrie & Kwaliteitszorg / team Biometrics & Quality Assurance
>> Kliniekstraat 25
>> 1070 Anderlecht
>> Belgium
>>
>> To call in the statistician after the experiment is done may be no more
>> than asking him to perform a post-mortem examination: he may be able to say
>> what the experiment died of. ~ Sir Ronald Aylmer Fisher
>> The plural of anecdote is not data. ~ Roger Brinner
>> The combination of some data and an aching desire for an answer does not
>> ensure that a reasonable answer can be extracted from a given body of data.
>> ~ John Tukey
>>
>> 2015-07-21 15:21 GMT+02:00 Emmanuel Levy <emmanuel.levy op gmail.com>:
>>
>>> Hi,
>>>
>>> The answer to this is probably straightforward, I have a dataframe and
>>> I'd
>>> like to build an index of column combinations, e.g.
>>>
>>> col1 col2  --> col3 (the index I need)
>>> A     1           1
>>> A     1           1
>>> A     2           2
>>> B     1           3
>>> B     2           4
>>> B     2           4
>>>
>>>
>>> At the moment I use:
>>> col3 <- apply(mat[,sel.col], 1, paste0)
>>>
>>> But I wonder if another approach could be faster?
>>>
>>> Thanks,
>>>
>>> Emmanuel
>>>
>>>         [[alternative HTML version deleted]]
>>>
>>> ______________________________________________
>>> R-help op r-project.org mailing list -- To UNSUBSCRIBE and more, see
>>> 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]]



More information about the R-help mailing list