Hi Laura,
The function merge() works well, but another elegant to do it would
involve match().
(Assuming your data.frame object is called x)
x$motherAge <- with(x, age[match(mothers.I.D,I.D)])
Cheers,
--
*Luc Villandré*
/Biostatistician
McGill University Health Center -
Montreal Children's Hospital Research Institute/
John Kane wrote:
x <- "I.D age 'MID'
01 5 03
02 6 06
03 16 NA
04 8 06
05 3 NA
06 17 NA"
xx <- read.table(textConnection(x), header=TRUE); xx
closeAllConnections()
ag1 <- xx[, c(1,2)] ; ag1
ag2 <- xx[, c(1,3)] ; ag2
names(ag2[2]) <- "I.D"
merge(ag1,ag2, by="I.D")
--- On Fri, 6/26/09, Kubasiewicz, Laura <laura.kubasiewic...@imperial.ac.uk>
wrote:
From: Kubasiewicz, Laura <laura.kubasiewic...@imperial.ac.uk>
Subject: [R] Matching data to a new column
To: "r-help@r-project.org" <r-help@r-project.org>
Received: Friday, June 26, 2009, 6:55 AM
Hi all,
I wonder if anyone can help,
I have a dataframe with columns for...
'I.D' 'age' 'mothers I.D'
01 5
03
02 6
06
03 16 NA
04 8
06
05 3
NA
06 17 NA
I need to create a new column for 'mothers age' which puts
the age of the individual with 'mothers i.d' into the row
for her offspring (so individual 01 would have 16 in the
mothers age column, as thats the age of individual 03)
Hope that makes sense, any help appreciated,
Thanks
Laura
[[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.
__________________________________________________________________
Be smarter than spam. See how smart SpamGuard i
ions in Mail and switch to New Mail today or register for free at
http://mail.yahoo.ca
______________________________________________
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.
______________________________________________
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.