Hi all, I would like to transform to long format a matrix which has only information about individuals and a value for each individual. I would like to have it in ling format with more information related to groups, so ell values and individuals are repeated for each group. Let me show the example: matrix in wide format: Individuals Value A 3 B 4 C 5 D 2 And long format would be like: Individuals Value Group A 3 Group1 B 4 Group1 C 5 Group1 D 2 Group1 A 3 Group2 B 4 Group2 C 5 Group2 D 2 Group2 My case is of course very large so i thought this could be possible with reshape function by adding a vector with the names of the groups or something like that. Thanks in advance. [[alternative HTML version deleted]]
check out the melt function in the reshape package. On Tue, Mar 3, 2009 at 8:53 AM, Usuario R <r.user.spain at gmail.com> wrote:> Hi all, > > I would like to transform to long format a matrix ?which has only > information about individuals and a value for each individual. I would like > to have it in ling format with more information related to groups, so ell > values and individuals are repeated for each group. Let me show the example: > > matrix in wide format: > > Individuals ? ?Value > A ? ?3 > B ? ?4 > C ? ?5 > D ? ?2 > > And long format would be like: > > Individuals ? ?Value ? ?Group > A ? ?3 ? ?Group1 > B ? ?4 ? ?Group1 > C ? ?5 ? ?Group1 > D ? ?2 ? ?Group1 > A ? ?3 ? ?Group2 > B ? ?4 ? ?Group2 > C ? ?5 ? ?Group2 > D ? ?2 ? ?Group2 > > My case is of course very large so i thought this could be possible with > reshape function by adding a vector with the names of the groups or > something like that. > > Thanks in advance. > > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > 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. >-- Stephen Sefick Let's not spend our time and resources thinking about things that are so little or so large that all they really do for us is puff us up and make us feel like gods. We are mammals, and have not exhausted the annoying little problems of being mammals. -K. Mullis
Your data objects could not be matrices with that composition of values. Let's use the correct data type: > df <- data.frame(Ind = letters[1:4], val = sample(1:4, 4)) > df Ind val 1 a 4 2 b 2 3 c 3 4 d 1 > rbind(cbind(df,"GruppeEin"),cbind(df,"GruppeZwei")) #v ariable recycling Ind val y 1 a 4 GruppeEin 2 b 2 GruppeEin 3 c 3 GruppeEin 4 d 1 GruppeEin 5 a 4 GruppeZwei 6 b 2 GruppeZwei 7 c 3 GruppeZwei 8 d 1 GruppeZwei > is.data.frame(rbind(cbind(df,"GruppeEin"),cbind(df,"GruppeZwei"))) [1] TRUE -- David Winsemius On Mar 3, 2009, at 8:53 AM, Usuario R wrote:> Hi all, > > I would like to transform to long format a matrix which has only > information about individuals and a value for each individual. I > would like > to have it in ling format with more information related to groups, > so ell > values and individuals are repeated for each group. Let me show the > example: > > matrix in wide format: > > Individuals Value > A 3 > B 4 > C 5 > D 2 > > And long format would be like: > > Individuals Value Group > A 3 Group1 > B 4 Group1 > C 5 Group1 > D 2 Group1 > A 3 Group2 > B 4 Group2 > C 5 Group2 > D 2 Group2 > > My case is of course very large so i thought this could be possible > with > reshape function by adding a vector with the names of the groups or > something like that. > > Thanks in advance. > > [[alternative HTML version deleted]] > > ______________________________________________ > 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.