[R] how to read a df like that and transform it?

Ista Zahn istazahn at gmail.com
Wed Jan 23 13:56:06 CET 2013


I don't really understand this data table, but maybe this modification
will give you the idea:


dat <- read.table(text="father  mother  num_daughter    daughter
291     3906    0       NA
275     4219    0       NA
273     4236    1       49410
281     4163    1       49408
274     4226    1       49406
295     3869    2       49403
287     4113    0       NA
295     3871    1       49401
292     3895    4       49396
291     3900    3       49392", header=TRUE)

library(reshape2)
dat$num_daughter <- paste0("daughter", dat$num_daughter)
dcast(dat, ... ~ num_daughter, value.var="daughter")


Best,
Ista
On Wed, Jan 23, 2013 at 7:42 AM, Yao He <yao.h.1988 at gmail.com> wrote:
> Dear all
>
> I have a data.frame like that :
>
> father  mother  num_daughter    daughter
> 291     3906    0       NULL
> 275     4219    0       NULL
> 273     4236    1       49410
> 281     4163    1       49408
> 274     4226    1       49406
> 295     3869    2       49403
>                                 49404
> 287     4113    0       NULL
> 295     3871    1       49401
> 292     3895    4       49396
>                                 49397
>                                 49398
>                                 49399
> 291     3900    3       49392
>
> How to read it into R and transform it like that:
>
> father mother   num_daughter   daughter1  daughter2  daughter3 daughter4
> 291     3906    0       NULL
> 275     4219    0       NULL
> 273     4236    1       49410
> 281     4163    1       49408
> 274     4226    1       49406
> 295     3869    2       49403      49404
> 287     4113    0       NULL
> 295     3871    1       49401
> 292     3895    4       49396      49397    49398   49399
> 291     3900    3       49392
>
> library (plyr) and library (reshape2) and other good packages are  OK for me.
>
> Thanks a lot!
>
> Yao He
> —————————————————————————
> Master candidate in 2rd year
> Department of Animal genetics & breeding
> Room 436,College of Animial Science&Technology,
> China Agriculture University,Beijing,100193
> E-mail: yao.h.1988 at gmail.com
> ——————————————————————————
>
> ______________________________________________
> 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.



More information about the R-help mailing list