[R] Reshape from long to wide

109 views
Skip to first unread message

aly

unread,
Mar 19, 2012, 7:01:09 PM3/19/12
to r-h...@r-project.org
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-h...@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.

Jorge I Velez

unread,
Mar 20, 2012, 12:29:08 AM3/20/12
to aly, r-h...@r-project.org
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.-

[[alternative HTML version deleted]]

Gabor Grothendieck

unread,
Mar 20, 2012, 12:37:30 AM3/20/12
to aly, r-h...@r-project.org

Here are a few solutions. First create fish2 from fish:

fish2 <- data.frame(fish, kid = c("kid1", "kid2"))

# 1
xtabs(length ~ family + kid, DF2)

# 2
reshape(data = DF2, dir = "wide", timevar = "kid", idvar = "family")

# 3
with(fish2, data.frame(family = unique(family),
kid1 = length[kid == "kid1"],
kid2 = length[kid == "kid2"]))

# 4
library(reshape)
cast(family ~ kid, data = fish2, value = "length")

# 5
library(reshape2)
dcast(family ~ kid, data = fish2, value.var = "length")

--
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com

Tyler Rinker

unread,
Mar 20, 2012, 12:41:51 AM3/20/12
to jorgeiv...@gmail.com, alya...@gmail.com, r-h...@r-project.org

Another approach, as your needs are very specific (take every other item in the second column and the unique values of columns 1) would be to index, use unique and put it together with data.frame (or cbind).

data.frame(family = unique(x[, 1]), kid1 = x[c(T, F), 2], kid2 = x[c(F, T), 2])

Cheers,Tyler

aly

unread,
Mar 21, 2012, 7:07:24 PM3/21/12
to r-h...@r-project.org

Thanks a lot,

I tried one of the ways you guys showed me and it totally work. Just for
fun, I tried all the others and with some modifications here and there they
work fine too. It was time consuming but definitely worth as a good learning
experience. Thanks again

--
View this message in context: http://r.789695.n4.nabble.com/Reshape-from-long-to-wide-tp4486875p4494055.html


Sent from the R help mailing list archive at Nabble.com.

Reply all
Reply to author
Forward
0 new messages