Hi aly,

Try

# your data
x <- structure(list(family = c(14L, 14L, 15L, 15L, 17L, 17L, 18L,
18L, 20L, 20L, 24L, 24L, 25L, 25L, 27L, 27L, 28L, 28L, 29L, 29L
), length = c(18L, 7L, 7L, 21L, 50L, 21L, 36L, 21L, 36L, 42L,
56L, 42L, 43L, 56L, 15L, 42L, 7L, 42L, 56L, 49L)), .Names = c("family",
"length"), class = "data.frame", row.names = c(NA, -20L))

# processing
require(plyr)
ddply(x, .(family), function(df) c(kid1 = df$length[1], kid2 =
df$length[2]))
   family kid1 kid2
1      14   18    7
2      15    7   21
3      17   50   21
4      18   36   21
5      20   36   42
6      24   56   42
7      25   43   56
8      27   15   42
9      28    7   42
10     29   56   49

HTH,
Jorge.-


On Mon, Mar 19, 2012 at 7:01 PM, aly <> wrote:

> Hi,
>
> I'm a total beginner in R and this question is probably very simple but
> I've
> spent hours reading about it and can't find the answer. I'm trying to
> reshape a data table from long to wide format. I've tried reshape() and
> cast() but I get error messages every time and I can't figure why. In my
> data, I have the length of two fish from each family. My data table (called
> fish) looks like this:
>
> family  length
> 14      18
> 14      7
> 15      7
> 15      21
> 17      50
> 17      21
> 18      36
> 18      21
> 20      36
> 20      42
> 24      56
> 24      42
> 25      43
> 25      56
> 27      15
> 27      42
> 28      7
> 28      42
> 29      56
> 29      49
>
> I want it to look like this:
>
> family kid1 kid2
> 14      18      7
> 15      7       21
> 17      50      21
> 18      36      21
> 28      36      42
> 24      56      42
> 25      43      56
> 27      15      42
> 28      7       42
> 29      56      49
>
> I've tried:
>
> >cast( fish, fam~length)
>
> and got the error message:
>
> Using length as value column.  Use the value argument to cast to override
> this choice
> Error in `[.data.frame`(data, , variables, drop = FALSE) :
>  undefined columns selected
>
> Then I rename the columns:
>
> >myvars<-c("fam","length")
> >fish<-fish[myvars]
>
> and try the cast() again with no luck (same error)
>
> By using reshape() I don't get the results I want:
>
> >reshape(rdm1, timevar="fam", idvar=c("length"), direction="wide")
> > head(first)
>               length
> 14.20        14
> 14.19         7
> 15.25        21
> 17.30        50
> 18.32        36
> 20.36        42
>
> Can someone help with this? Thanks a lot!
>
>
>
>
> --
> View this message in context:
> http://r.789695.n4.nabble.com/Reshape-from-long-to-wide-tp4486875p4486875.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help@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.
>

        [[alternative HTML version deleted]]

______________________________________________
R-help@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.

Reply via email to