[R] Reshape

baptiste auguie ba208 at exeter.ac.uk
Tue Mar 3 15:43:00 CET 2009


If you have many such repetitions it can be annoying to type the  
rbind(cbind ... sequence. Perhaps this would help,


> m <- read.table(textConnection("
> Individuals    Value
> A    3
> B    4
> C    5
> D    2"), head=T)
>
> data.frame(groups= rep(c("group1", "group2", "group3"),  
> each=nrow(m)), Ind=m[, 1], val=m[, 2])


You could also try Hadley's plyr package,

> rdply(3,m) # you might want to rename the column name and make(.n) a  
> factor with adequate levels



baptiste

On 3 Mar 2009, at 14:28, David Winsemius wrote:

> 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.
>
> ______________________________________________
> 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.

_____________________________

Baptiste Auguié

School of Physics
University of Exeter
Stocker Road,
Exeter, Devon,
EX4 4QL, UK

Phone: +44 1392 264187

http://newton.ex.ac.uk/research/emag




More information about the R-help mailing list