[R] FW: Help on reshape2 data frame rearrangement

B Jessop deelman at hotmail.com
Mon Aug 8 17:47:33 CEST 2011


Help-list,

 

Sorry about the formatting problem, Josh.  The following example data set(minor difference from original) in plain text should make the problem clear.



Original data format:
Site     Fish_no   Length
Site1    Fish1        10
Site1    Fish2        13
Site1    Fish3        14
Site1    Fish4        15
……….
Site2    Fish1        5
Site2    Fish2        8
Site2    Fish3        7
Site2    Fish4        9
……..
Site and Fish are factors, Length is a numeric variable.  There are 15 cases for each Site group.  The objective is to reshape to:
Site1    Site2
10        5
13        8
14        7
15        9
Etc.
The following code works inadequately:
Df2 <- dcast (Data1, Length ~ Site, value_var = “Length”) 


It produces the following:
Length   Site1   Site2
10       10       NA
13       13       NA
14       14       NA
15       15       NA 
……..
5         NA       5
8         NA       8
7         NA       7
……..

 

Thanks for any suggestions.

 

Regards,

BJ
----------------------------------------
> Date: Sun, 7 Aug 2011 18:27:49 -0700
> Subject: Re: [R] Help on reshape2 data frame rearrangement
> From: jwiley.psych at gmail.com
> To: deelman at hotmail.com
> CC: r-help at r-project.org
>
> Hi BJ,
>
> Suppose that your data set is called 'Data1', please copy and paste
> the output created by typing:
>
> dput(Data1[1:15, ])
>
> and post that to the list ***in plain text*** (or upload a text file
> on some file hosting service). The email you sent is nearly
> impossible to parse because this is a plain text list (as noted in the
> posting guide) and your html formatting is utterly lost.
>
> Josh
>
> On Sun, Aug 7, 2011 at 6:19 PM, B Jessop <deelman at hotmail.com> wrote:
> >
> >
> > Dear help list: I am trying to reshape a data frame from long to wide format and with a reduced variable list using reshape2. The original data frame format is: 

Site    Obs_no   Length

Site 1 Obs 1     10

Site 1 Obs 2     13

Site 1 Obs 3     14

.........

Site 2 Obs 1     5

Site 2 Obs 2     7

Site 2 Obs 3     9 



Site and Obs_no are factors and Length is a numeric variable. There are 15 cases in each Site group. The objective is to rearrange to: 

Site 1 Site 2

10      5

13      7

14      9 

........ 

I have tried a variety of approaches, none of which worked. The following code does not work but suggests the task might be possible, perhaps in 2 steps. 

Df2 <- dcast (Data1, Length ~ Site, value_var = "Length") Its output is:

Length Site 1 Site2

10      10     NA

13      13     NA

14      14     NA

.......

5       NA     5

7       NA     7

9       NA     9



[[elided Hotmail spam]]
> > r use but I would like to do it in R. Any suggestions would be much appreciated. Regards,BJ
> > [[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.
> >
>
>
>
> --
> Joshua Wiley
> Ph.D. Student, Health Psychology
> Programmer Analyst II, ATS Statistical Consulting Group
> University of California, Los Angeles
> https://joshuawiley.com/ 		 	   		  


More information about the R-help mailing list