# [R] Calculate interaction for a big dataframe

arun smartpink111 at yahoo.com
Mon Jul 22 17:52:05 CEST 2013

```Hi,
You could try:
cumsum(c(1,abs(diff(as.numeric(factor(v1))))))
#  1 1 1 2 2 3 3 3 3 4
A.K.

----- Original Message -----
From: Arnaud Michel <michel.arnaud at cirad.fr>
To: PIKAL Petr <petr.pikal at precheza.cz>
Cc: R help <r-help at r-project.org>
Sent: Monday, July 22, 2013 11:41 AM
Subject: Re: [R] Calculate interaction for a big dataframe

Thank you Petr
paste is better than interaction for long vectors
But now a new problem/question is appeared.
Now, I would like transform the vector
v1 <- c(
"4162.France", "4162.France", "4162.France",
"4162.Mali", "4162.Mali",
"4162.France", "4162.France", "4162.France", "4162.France",
"4162.Mali")
into a vector V2 with the same length but with number which are creasing
v2 <- c(1,     1,    1,
2, 2,
3, 3,3,3,
4))

Any idea (function) ?
Regards

Le 22/07/2013 14:45, PIKAL Petr a écrit :
> Hi
>
> you maybe could use paste
>
>> f1<-sample(letters[1:3], 10, replace=T)
>> f2<-sample(letters[4:7], 10, replace=T)
>> f3<-sample(letters[9:11], 10, replace=T)
>> interaction(f1, f2, f3, drop=T)
>    c.e.j b.e.j a.e.j c.g.i a.f.j b.g.k a.e.i a.e.k a.d.j b.e.j
> Levels: a.e.i c.g.i a.d.j a.e.j b.e.j c.e.j a.f.j a.e.k b.g.k
>> paste(f1, f2, f3, sep=".")
>    "c.e.j" "b.e.j" "a.e.j" "c.g.i" "a.f.j" "b.g.k" "a.e.i" "a.e.k" "a.d.j"
>  "b.e.j"
>
> The difference is that interaction gives you directly factor, paste gives you character vector, but it may be convenient too for your purpose.
>
> Regards
> Petr
>
>
>> -----Original Message-----
>> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
>> project.org] On Behalf Of Arnaud Michel
>> Sent: Monday, July 22, 2013 10:57 AM
>> To: R help
>> Subject: [R] Calculate interaction for a big dataframe
>>
>> Hi
>>
>> To calculate the value of the interaction between factors of a
>> dataframe df, does exist any function which could replace the function
>> when the dataframe df has the numbers of rows of df is large (~55000)
>> and also the numbers of combinaison of the three factors is large. The
>> calcul abort.
>> The function to calculate the interaction is :
>> as.numeric(interaction(df [,c(1:3)],drop=TRUE))
>>
>> To complete the question and to calculate interaction beetween 3
>> factors f1, f2, f3, does it possible to calculate first f12 =
>> interaction
>> (f1,f2) and after calculate interaction (f12, f3).
>> It seems to me that yes.
>>
>>
>>
>>
>>
>> --
>> Michel ARNAUD
>> Chargé de mission auprès du DRH
>> DGDRD-Drh - TA 174/04
>> Av Agropolis 34398 Montpellier cedex 5
>> tel : 04.67.61.75.38
>> fax : 04.67.61.57.87
>> port: 06.47.43.55.31
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> guide.html
>> and provide commented, minimal, self-contained, reproducible code.

--
Michel ARNAUD
Chargé de mission auprès du DRH
DGDRD-Drh - TA 174/04
Av Agropolis 34398 Montpellier cedex 5
tel : 04.67.61.75.38
fax : 04.67.61.57.87
port: 06.47.43.55.31

______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help