Dear R users The following question looks simple but I have spend alot of time to solve it. I would highly appeciate your help. I have following dataset from family dataset : Here we have individuals and their two parents and their marker scores (marker1, marker2,....and so on). 0 means that their parent information not available. Individual Parent1 Parent2 mark1 mark2 1 0 0 12 11 2 0 0 11 22 3 0 0 13 22 4 0 0 13 11 5 1 2 11 12 6 1 2 12 12 7 3 4 11 12 8 3 4 13 12 9 1 4 11 12 10 1 4 11 12 I want to recode mark1 and other mark2.....and so on column by looking indvidual parent (Parent1 and Parent2). For example Take case of Individual 5, who's Parent 1 is 1 (has mark1 score 12) and Parent 2 is 2 (has mark1 score 11). Individual 5 has mark1 score 11. Suppose I have following condition to recode Individual 5's mark1 score: For mark1 variable, If Parent1 score "11" and Parent2 score "22" and recode indvidual 5's score, "12"=1, else 0 If Parent1 score "12" and Parent2 score "22" and recode individual 5's score, "22"=1, "12"= 0.5, else 0 .........................more conditions Similarly the pointer should move from individual 5 to n individuals at the end of the file. Thank you in advance Umesh R [[alternative HTML version deleted]]
Dear Umesh, I could not figure out exactly what your recoding scheme was, so I do not have a specific solution for you. That said, the following functions may help you get started. ?ifelse # vectorized and different from using if () statements ?if # ?Logic ## logical operators for your tests ## if you install and load the "car" package by John Fox ?recode # a function for recoding in package "car" I am sure it is possible to string together some massive series of if statements and then use a for loop, but that is probably the messiest and slowest possible way. I suspect there will be faster, neater options, but I cannot say for certain without having a better feel for how all the conditions work. Best regards, Josh On Thu, Feb 17, 2011 at 6:21 PM, Umesh Rosyara <rosyaraur at gmail.com> wrote:> Dear R users > > The following question looks simple but I have spend alot of time to solve > it. I would highly appeciate your help. > > I have following dataset from family dataset : > > Here we have individuals and their two parents and their marker scores > (marker1, marker2,....and so on). 0 means that their parent information not > available. > > > Individual ? ? ?Parent1 ?Parent2 ? ? ? ? mark1 ? mark2 > 1 ? ? ? ?0 ? ? ? 0 ? ? ? 12 ? ? ?11 > 2 ? ? ? ?0 ? ? ? 0 ? ? ? 11 ? ? ?22 > 3 ? ? ? ?0 ? ? ? 0 ? ? ? 13 ? ? ?22 > 4 ? ? ? ?0 ? ? ? 0 ? ? ? 13 ? ? ?11 > 5 ? ? ? ?1 ? ? ? 2 ? ? ? 11 ? ? ?12 > 6 ? ? ? ?1 ? ? ? 2 ? ? ? 12 ? ? ?12 > 7 ? ? ? ?3 ? ? ? 4 ? ? ? 11 ? ? ?12 > 8 ? ? ? ?3 ? ? ? 4 ? ? ? 13 ? ? ?12 > 9 ? ? ? ?1 ? ? ? 4 ? ? ? 11 ? ? ?12 > 10 ? ? ? 1 ? ? ? 4 ? ? ? 11 ? ? ?12 > > I want to recode mark1 and other mark2.....and so on column by looking > indvidual parent (Parent1 and Parent2). > > For example > > Take case of Individual 5, who's Parent 1 is 1 (has mark1 score 12) and > Parent 2 is 2 (has mark1 score 11). Individual 5 has mark1 score 11. Suppose > I have following condition to recode Individual 5's mark1 score: > > For mark1 variable, If Parent1 score "11" and Parent2 score "22" and recode > indvidual 5's score, "12"=1, else 0 > ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?If Parent1 score "12" and Parent2 score > "22" and recode individual 5's score, "22"=1, "12"= 0.5, else 0 > ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?.........................more conditions > > Similarly the pointer should move from individual 5 to n individuals at the > end of the file. > > ?Thank you in advance > > Umesh R > > > > > > ? ? ? ?[[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. >-- Joshua Wiley Ph.D. Student, Health Psychology University of California, Los Angeles http://www.joshuawiley.com/
Hi: This is as far as I could get: df <- read.table(textConnection(" Individual Parent1 Parent2 mark1 mark2 1 0 0 12 11 2 0 0 11 22 3 0 0 13 22 4 0 0 13 11 5 1 2 11 12 6 1 2 12 12 7 3 4 11 12 8 3 4 13 12 9 1 4 11 12 10 1 4 11 12"), header = TRUE) df2 <- transform(df, Parent1 = replace(Parent1, Parent1 == 0, NA), Parent2 = replace(Parent2, Parent2 == 0, NA)) df2 <- transform(df2, imark1p1 = df2$mark1[df2$Parent1], # Parent 1's mark1 imark1p2 = df2$mark1[df2$Parent2], # Parent 2's mark1 imark2p1 = df2$mark2[df2$Parent1], # Parent 1's mark2 imark2p2 = df2$mark2[df2$Parent2]) # Parent 2's mark2 I created df2 so as not to overwrite the original in case of a mistake. At this point, you have several sets of vectors that you can compare; e.g., mark1 with imark1p1 and imark1p2. Like Josh, I couldn't make heads or tails out of what these logical tests were meant to output, but perhaps this gives you a broader template with which to work. At this point, you can probably remove the rows corresponding to the parents. I believe ifelse() is your friend here - it can perform logical tests in a vectorized fashion. As long as the tests are consistent from one individual to the next, it's likely to be an efficient route. HTH, Dennis On Thu, Feb 17, 2011 at 6:21 PM, Umesh Rosyara <rosyaraur@gmail.com> wrote:> Dear R users > > The following question looks simple but I have spend alot of time to solve > it. I would highly appeciate your help. > > I have following dataset from family dataset : > > Here we have individuals and their two parents and their marker scores > (marker1, marker2,....and so on). 0 means that their parent information not > available. > > > Individual Parent1 Parent2 mark1 mark2 > 1 0 0 12 11 > 2 0 0 11 22 > 3 0 0 13 22 > 4 0 0 13 11 > 5 1 2 11 12 > 6 1 2 12 12 > 7 3 4 11 12 > 8 3 4 13 12 > 9 1 4 11 12 > 10 1 4 11 12 > > I want to recode mark1 and other mark2.....and so on column by looking > indvidual parent (Parent1 and Parent2). > > For example > > Take case of Individual 5, who's Parent 1 is 1 (has mark1 score 12) and > Parent 2 is 2 (has mark1 score 11). Individual 5 has mark1 score 11. > Suppose > I have following condition to recode Individual 5's mark1 score: > > For mark1 variable, If Parent1 score "11" and Parent2 score "22" and recode > indvidual 5's score, "12"=1, else 0 > If Parent1 score "12" and Parent2 score > "22" and recode individual 5's score, "22"=1, "12"= 0.5, else 0 > .........................more conditions > > Similarly the pointer should move from individual 5 to n individuals at the > end of the file. > > Thank you in advance > > Umesh R > > > > > > [[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. >[[alternative HTML version deleted]]
Hi Dennis I was able to my problem. Thank you encouragement and time. n<-7 newvars <- c(paste('m', rep(1:n, each = 4), rep(c('a', 'b')), rep(c('p1', 'p2'), each = 2), sep = '')) newvars [1] "m1ap1" "m1bp1" "m1ap2" "m1bp2" "m2ap1" "m2bp1" "m2ap2" "m2bp2" "m3ap1" [10] "m3bp1" "m3ap2" "m3bp2" "m4ap1" "m4bp1" "m4ap2" "m4bp2" "m5ap1" "m5bp1" [19] "m5ap2" "m5bp2" "m6ap1" "m6bp1" "m6ap2" "m6bp2" "m7ap1" "m7bp1" "m7ap2" [28] "m7bp2" Umesh R [[alternative HTML version deleted]]