[R] appending data to a row

Rui Barradas ruipbarradas at sapo.pt
Thu Feb 14 22:39:18 CET 2013


Hello,

Maybe something like the following.


dat <- read.table(text = "
FamilyID  ParticipantID  IQ  Digit_span
1             1          95  6
1             2          93  7
2             3          102 8
2             4          101 9
3             5          106 7
3             6          108 9
", header = TRUE)
dat$timevar <- rep(1:2, nrow(dat)/2)
reshape(dat, idvar = "FamilyID", timevar = "timevar", direction = "wide")



See ?reshape.

Hope this helps,

Rui Barradas

Em 14-02-2013 21:07, Jamie escreveu:
> For an analysis of data from twins, I'd like to do some rearranging in my
> dataframe. Currently, each twin has his or her own row. I'd like to add
> several columns of the co-twin's data to each row. In other words, this is
> the current situation:
>
> FamilyID  ParticipantID  IQ  Digit_span
> 1             1          95  6
> 1             2          93  7
> 2             3          102 8
> 2             4          101 9
> 3             5          106 7
> 3             6          108 9
>
> And this is what I'd like to have, only with even more co-twin data:
>
> FamilyID  ParticipantID  IQ  Digit_span Co-twin_IQ Co-twin_Digit_span
> 1             1          95  6          93          7
> 1             2          93  7          95          6
> 2             3          102 8          101         9
> 2             4          101 9          102         8
> 3             5          106 7          108         9
> 3             6          108 9          106         7
>
> I'm not sure how to tell R to take the specified values from the other row
> with the same family ID and append them with a new name. Thanks for any
> ideas you can offer me.
>
> Jamie in Illinois
>
> 	[[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.
>



More information about the R-help mailing list