Re: [R] the first. from SAS in R
My apologies for coming to the party so late. I'm sure this question has been answered a couple of times. The attached function is one I pulled from the help archives, but I can't seem to duplicate the search that led me to it. In any case, I've attached the function I found, and an .Rd file I use as part of a local package. I've also attached a pair of accompanying records to retrieve the last record and the nth record. These have the advantage of not requiring data frames to be sorted prior to extraction--the function will sort them for you. Benjamin -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of David Katz Sent: Wednesday, November 24, 2010 10:17 AM To: r-help@r-project.org Subject: Re: [R] the first. from SAS in R Often the purpose of first/last in sas is to facilitate grouping of observations in a sequential algorithm. This purpose is better served in R by using vectorized methods like those in package plyr. Also, note that first/last has different meanings in the context of by x; versus by x notsorted;. R duplicated does not address the latter, which splits noncontiguous records with equal x. Regards, David -- View this message in context: http://r.789695.n4.nabble.com/the-first-from-SAS-in-R-tp3055417p3057476. html Sent from the R help mailing list archive at Nabble.com. __ 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. === P Please consider the environment before printing this e-mail Cleveland Clinic is ranked one of the top hospitals in America by U.S.News World Report (2009). Visit us online at http://www.clevelandclinic.org for a complete listing of our services, staff and locations. Confidentiality Note: This message is intended for use only by the individual or entity to which it is addressed and may contain information that is privileged, confidential, and exempt from disclosure under applicable law. If the reader of this message is not the intended recipient or the employee or agent responsible for delivering the message to the intended recipient, you are hereby notified that any dissemination, distribution or copying of this communication is strictly prohibited. If you have received this communication in error, please contact the sender immediately and destroy the material in its entirety, whether electronic or hard copy. Thank you. __ 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.
Re: [R] the first. from SAS in R
Often the purpose of first/last in sas is to facilitate grouping of observations in a sequential algorithm. This purpose is better served in R by using vectorized methods like those in package plyr. Also, note that first/last has different meanings in the context of by x; versus by x notsorted;. R duplicated does not address the latter, which splits noncontiguous records with equal x. Regards, David -- View this message in context: http://r.789695.n4.nabble.com/the-first-from-SAS-in-R-tp3055417p3057476.html Sent from the R help mailing list archive at Nabble.com. __ 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.
Re: [R] the first. from SAS in R
Perhaps something like this: a$d - ifelse(duplicated(a$a), 0, 1) On Tue, Nov 23, 2010 at 1:33 PM, Joel joda2...@student.uu.se wrote: Is there any similar function in R to the first. in SAS? What it dose is: Lets say we have this table: a b c 1 1 5 1 0 2 2 0 2 2 0 NA 2 9 2 3 1 3 and then I want do to do one thing the first time the number 1 appers in a and something else the secund time 1 appers in a and so on. so something similar to: if first.a { a$d-1 }else{ a$d-0 } This would give me a b c b 1 1 5 1 1 0 2 0 2 0 2 1 2 0 NA 0 2 9 2 0 3 1 3 1 Is there such a function in R or anything similar? thx //Joel -- View this message in context: http://r.789695.n4.nabble.com/the-first-from-SAS-in-R-tp3055417p3055417.html Sent from the R help mailing list archive at Nabble.com. __ 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.
Re: [R] the first. from SAS in R
On Nov 23, 2010, at 8:33 AM, Joel wrote: Is there any similar function in R to the first. in SAS? What it dose is: Lets say we have this table: a b c 1 1 5 1 0 2 2 0 2 2 0 NA 2 9 2 3 1 3 and then I want do to do one thing the first time the number 1 appers in a and something else the secund time 1 appers in a and so on. so something similar to: if first.a { a$d-1 }else{ a$d-0 } The duplicated function which returns a logical vector with those features can easily be coerced to numeric. df$d - as.numeric(!duplicated(df$a)) I was a bit puzzled about my failure to get coercion by the method which I thought was supposed to work, namely adding 0. df$e - !duplicated(df$a)+0 # does not coerce df$e - 0 + !duplicated(df$a) # pre-adding 0 does coerce Maybe the rules on coercion were amended. -- David This would give me a b c b 1 1 5 1 1 0 2 0 2 0 2 1 2 0 NA 0 2 9 2 0 3 1 3 1 Is there such a function in R or anything similar? thx //Joel -- View this message in context: http://r.789695.n4.nabble.com/the-first-from-SAS-in-R-tp3055417p3055417.html Sent from the R help mailing list archive at Nabble.com. __ 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. David Winsemius, MD West Hartford, CT __ 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.
Re: [R] the first. from SAS in R
Interesting. Check this out: u - sample(c(TRUE, FALSE), 10, replace = TRUE) u [1] FALSE FALSE TRUE FALSE FALSE TRUE FALSE FALSE FALSE FALSE class(u) [1] logical u + 0 [1] 0 0 1 0 0 1 0 0 0 0 0 + u [1] 0 0 1 0 0 1 0 0 0 0 v - rpois(10, 3) !duplicated(v) [1] TRUE FALSE TRUE TRUE TRUE TRUE FALSE TRUE FALSE TRUE class(!duplicated(v)) [1] logical !duplicated(v) + 0 [1] TRUE FALSE TRUE TRUE TRUE TRUE FALSE TRUE FALSE TRUE 0 + !duplicated(v) [1] 1 0 1 1 1 1 0 1 0 1 # Now assign !duplicated(v) to an object: w - !duplicated(v) class(w) [1] logical 0 + w [1] 1 0 1 1 1 1 0 1 0 1 w + 0 [1] 1 0 1 1 1 1 0 1 0 1 I can see *what* is going on, but what is the reason for it? I see another notebook entry coming :) Dennis On Tue, Nov 23, 2010 at 6:12 AM, David Winsemius dwinsem...@comcast.netwrote: On Nov 23, 2010, at 8:33 AM, Joel wrote: Is there any similar function in R to the first. in SAS? What it dose is: Lets say we have this table: a b c 1 1 5 1 0 2 2 0 2 2 0 NA 2 9 2 3 1 3 and then I want do to do one thing the first time the number 1 appers in a and something else the secund time 1 appers in a and so on. so something similar to: if first.a { a$d-1 }else{ a$d-0 } The duplicated function which returns a logical vector with those features can easily be coerced to numeric. df$d - as.numeric(!duplicated(df$a)) I was a bit puzzled about my failure to get coercion by the method which I thought was supposed to work, namely adding 0. df$e - !duplicated(df$a)+0 # does not coerce df$e - 0 + !duplicated(df$a) # pre-adding 0 does coerce Maybe the rules on coercion were amended. -- David This would give me a b c b 1 1 5 1 1 0 2 0 2 0 2 1 2 0 NA 0 2 9 2 0 3 1 3 1 Is there such a function in R or anything similar? thx //Joel -- View this message in context: http://r.789695.n4.nabble.com/the-first-from-SAS-in-R-tp3055417p3055417.html Sent from the R help mailing list archive at Nabble.com. __ 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. David Winsemius, MD West Hartford, CT __ 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]] __ 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.
Re: [R] the first. from SAS in R
On Tue, 23 Nov 2010, Joel wrote: Is there any similar function in R to the first. in SAS? What it dose is: Lets say we have this table: a b c 1 1 5 1 0 2 2 0 2 2 0 NA 2 9 2 3 1 3 and then I want do to do one thing the first time the number 1 appers in a and something else the secund time 1 appers in a and so on. so something similar to: if first.a { a$d-1 }else{ a$d-0 } This would give me a b c b 1 1 5 1 1 0 2 0 2 0 2 1 2 0 NA 0 2 9 2 0 3 1 3 1 Is there such a function in R or anything similar? See ?duplicated then try a$d - ifelse( duplicated( a$a ), 0 , 1 ) and a$d.2 - as.numeric( !duplicated( a$a ) ) HTH, Chuck thx //Joel -- View this message in context: http://r.789695.n4.nabble.com/the-first-from-SAS-in-R-tp3055417p3055417.html Sent from the R help mailing list archive at Nabble.com. __ 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. Charles C. BerryDept of Family/Preventive Medicine cbe...@tajo.ucsd.eduUC San Diego http://famprevmed.ucsd.edu/faculty/cberry/ La Jolla, San Diego 92093-0901 __ 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.
Re: [R] the first. from SAS in R
On Tue, 23 Nov 2010, Dennis Murphy wrote: Interesting. Check this out: u - sample(c(TRUE, FALSE), 10, replace = TRUE) u [1] FALSE FALSE TRUE FALSE FALSE TRUE FALSE FALSE FALSE FALSE class(u) [1] logical u + 0 [1] 0 0 1 0 0 1 0 0 0 0 0 + u [1] 0 0 1 0 0 1 0 0 0 0 v - rpois(10, 3) !duplicated(v) [1] TRUE FALSE TRUE TRUE TRUE TRUE FALSE TRUE FALSE TRUE class(!duplicated(v)) [1] logical !duplicated(v) + 0 [1] TRUE FALSE TRUE TRUE TRUE TRUE FALSE TRUE FALSE TRUE 0 + !duplicated(v) [1] 1 0 1 1 1 1 0 1 0 1 # Now assign !duplicated(v) to an object: w - !duplicated(v) class(w) [1] logical 0 + w [1] 1 0 1 1 1 1 0 1 0 1 w + 0 [1] 1 0 1 1 1 1 0 1 0 1 I can see *what* is going on, but what is the reason for it? I see another notebook entry coming :) See ?Arithmetic and read the paragraph under Details starting 'Logical vectors' Chuck Dennis On Tue, Nov 23, 2010 at 6:12 AM, David Winsemius dwinsem...@comcast.netwrote: On Nov 23, 2010, at 8:33 AM, Joel wrote: Is there any similar function in R to the first. in SAS? What it dose is: Lets say we have this table: a b c 1 1 5 1 0 2 2 0 2 2 0 NA 2 9 2 3 1 3 and then I want do to do one thing the first time the number 1 appers in a and something else the secund time 1 appers in a and so on. so something similar to: if first.a { a$d-1 }else{ a$d-0 } The duplicated function which returns a logical vector with those features can easily be coerced to numeric. df$d - as.numeric(!duplicated(df$a)) I was a bit puzzled about my failure to get coercion by the method which I thought was supposed to work, namely adding 0. df$e - !duplicated(df$a)+0 # does not coerce df$e - 0 + !duplicated(df$a) # pre-adding 0 does coerce Maybe the rules on coercion were amended. -- David This would give me a b c b 1 1 5 1 1 0 2 0 2 0 2 1 2 0 NA 0 2 9 2 0 3 1 3 1 Is there such a function in R or anything similar? thx //Joel -- View this message in context: http://r.789695.n4.nabble.com/the-first-from-SAS-in-R-tp3055417p3055417.html Sent from the R help mailing list archive at Nabble.com. __ 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. David Winsemius, MD West Hartford, CT __ 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]] __ 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. Charles C. BerryDept of Family/Preventive Medicine cbe...@tajo.ucsd.eduUC San Diego http://famprevmed.ucsd.edu/faculty/cberry/ La Jolla, San Diego 92093-0901 __ 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.
Re: [R] the first. from SAS in R
It all has to do with the precedence of the ! operator. Compare !duplicated(v) + 0 with (!duplicated(v)) + 0 Dave From: Charles C. Berry cbe...@tajo.ucsd.edu To: Dennis Murphy djmu...@gmail.com Cc: r-help@r-project.org Date: 11/23/2010 10:08 AM Subject: Re: [R] the first. from SAS in R Sent by: r-help-boun...@r-project.org On Tue, 23 Nov 2010, Dennis Murphy wrote: Interesting. Check this out: u - sample(c(TRUE, FALSE), 10, replace = TRUE) u [1] FALSE FALSE TRUE FALSE FALSE TRUE FALSE FALSE FALSE FALSE class(u) [1] logical u + 0 [1] 0 0 1 0 0 1 0 0 0 0 0 + u [1] 0 0 1 0 0 1 0 0 0 0 v - rpois(10, 3) !duplicated(v) [1] TRUE FALSE TRUE TRUE TRUE TRUE FALSE TRUE FALSE TRUE class(!duplicated(v)) [1] logical !duplicated(v) + 0 [1] TRUE FALSE TRUE TRUE TRUE TRUE FALSE TRUE FALSE TRUE 0 + !duplicated(v) [1] 1 0 1 1 1 1 0 1 0 1 # Now assign !duplicated(v) to an object: w - !duplicated(v) class(w) [1] logical 0 + w [1] 1 0 1 1 1 1 0 1 0 1 w + 0 [1] 1 0 1 1 1 1 0 1 0 1 I can see *what* is going on, but what is the reason for it? I see another notebook entry coming :) See ?Arithmetic and read the paragraph under Details starting 'Logical vectors' Chuck Dennis On Tue, Nov 23, 2010 at 6:12 AM, David Winsemius dwinsem...@comcast.netwrote: On Nov 23, 2010, at 8:33 AM, Joel wrote: Is there any similar function in R to the first. in SAS? What it dose is: Lets say we have this table: a b c 1 1 5 1 0 2 2 0 2 2 0 NA 2 9 2 3 1 3 and then I want do to do one thing the first time the number 1 appers in a and something else the secund time 1 appers in a and so on. so something similar to: if first.a { a$d-1 }else{ a$d-0 } The duplicated function which returns a logical vector with those features can easily be coerced to numeric. df$d - as.numeric(!duplicated(df$a)) I was a bit puzzled about my failure to get coercion by the method which I thought was supposed to work, namely adding 0. df$e - !duplicated(df$a)+0 # does not coerce df$e - 0 + !duplicated(df$a) # pre-adding 0 does coerce Maybe the rules on coercion were amended. -- David This would give me a b c b 1 1 5 1 1 0 2 0 2 0 2 1 2 0 NA 0 2 9 2 0 3 1 3 1 Is there such a function in R or anything similar? thx //Joel -- View this message in context: http://r.789695.n4.nabble.com/the-first-from-SAS-in-R-tp3055417p3055417.html Sent from the R help mailing list archive at Nabble.com. __ 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. David Winsemius, MD West Hartford, CT __ 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]] __ 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. Charles C. BerryDept of Family/Preventive Medicine cbe...@tajo.ucsd.edu UC San Diego http://famprevmed.ucsd.edu/faculty/cberry/ La Jolla, San Diego 92093-0901 __ 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]] __ 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.
Re: [R] the first. from SAS in R
On Nov 23, 2010, at 11:04 AM, Charles C. Berry wrote: On Tue, 23 Nov 2010, Dennis Murphy wrote: Interesting. Check this out: u - sample(c(TRUE, FALSE), 10, replace = TRUE) u [1] FALSE FALSE TRUE FALSE FALSE TRUE FALSE FALSE FALSE FALSE class(u) [1] logical u + 0 [1] 0 0 1 0 0 1 0 0 0 0 0 + u [1] 0 0 1 0 0 1 0 0 0 0 v - rpois(10, 3) !duplicated(v) [1] TRUE FALSE TRUE TRUE TRUE TRUE FALSE TRUE FALSE TRUE class(!duplicated(v)) [1] logical !duplicated(v) + 0 [1] TRUE FALSE TRUE TRUE TRUE TRUE FALSE TRUE FALSE TRUE 0 + !duplicated(v) [1] 1 0 1 1 1 1 0 1 0 1 # Now assign !duplicated(v) to an object: w - !duplicated(v) class(w) [1] logical 0 + w [1] 1 0 1 1 1 1 0 1 0 1 w + 0 [1] 1 0 1 1 1 1 0 1 0 1 I can see *what* is going on, but what is the reason for it? I see another notebook entry coming :) See ?Arithmetic and read the paragraph under Details starting 'Logical vectors' Chuck; Compare these three, all of which are using binary operators on logical vectors which is what is being discussed in ?Arithmetic: duplicated(c(a, a, b) ) + 0 [1] 0 1 0 !duplicated(c(a, a, b) ) + 0 [1] TRUE FALSE TRUE 0 + !duplicated(c(a, a, b) ) [1] 1 0 1 I believe the proper place to go is ?Syntax where operator precedence is discussed. I think the precendence rules implicitly do this in the second instance, because + has higher precendence than negation: ! ( duplicated(c(a, a, b) ) + 0 ) -- David. Chuck Dennis On Tue, Nov 23, 2010 at 6:12 AM, David Winsemius dwinsem...@comcast.net wrote: On Nov 23, 2010, at 8:33 AM, Joel wrote: Is there any similar function in R to the first. in SAS? What it dose is: Lets say we have this table: a b c 1 1 5 1 0 2 2 0 2 2 0 NA 2 9 2 3 1 3 and then I want do to do one thing the first time the number 1 appers in a and something else the secund time 1 appers in a and so on. so something similar to: if first.a { a$d-1 }else{ a$d-0 } The duplicated function which returns a logical vector with those features can easily be coerced to numeric. df$d - as.numeric(!duplicated(df$a)) I was a bit puzzled about my failure to get coercion by the method which I thought was supposed to work, namely adding 0. df$e - !duplicated(df$a)+0 # does not coerce df$e - 0 + !duplicated(df$a) # pre-adding 0 does coerce Maybe the rules on coercion were amended. -- David This would give me a b c b 1 1 5 1 1 0 2 0 2 0 2 1 2 0 NA 0 2 9 2 0 3 1 3 1 Is there such a function in R or anything similar? thx //Joel -- View this message in context: http://r.789695.n4.nabble.com/the-first-from-SAS-in-R-tp3055417p3055417.html Sent from the R help mailing list archive at Nabble.com. __ 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. David Winsemius, MD West Hartford, CT __ 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]] __ 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. Charles C. BerryDept of Family/ Preventive Medicine cbe...@tajo.ucsd.eduUC San Diego http://famprevmed.ucsd.edu/faculty/cberry/ La Jolla, San Diego 92093-0901 David Winsemius, MD West Hartford, CT __ 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.
Re: [R] the first. from SAS in R
On Tue, 23 Nov 2010, David Winsemius wrote: On Nov 23, 2010, at 11:04 AM, Charles C. Berry wrote: On Tue, 23 Nov 2010, Dennis Murphy wrote: Interesting. Check this out: u - sample(c(TRUE, FALSE), 10, replace = TRUE) u [1] FALSE FALSE TRUE FALSE FALSE TRUE FALSE FALSE FALSE FALSE class(u) [1] logical u + 0 [1] 0 0 1 0 0 1 0 0 0 0 0 + u [1] 0 0 1 0 0 1 0 0 0 0 v - rpois(10, 3) !duplicated(v) [1] TRUE FALSE TRUE TRUE TRUE TRUE FALSE TRUE FALSE TRUE class(!duplicated(v)) [1] logical !duplicated(v) + 0 [1] TRUE FALSE TRUE TRUE TRUE TRUE FALSE TRUE FALSE TRUE 0 + !duplicated(v) [1] 1 0 1 1 1 1 0 1 0 1 # Now assign !duplicated(v) to an object: w - !duplicated(v) class(w) [1] logical 0 + w [1] 1 0 1 1 1 1 0 1 0 1 w + 0 [1] 1 0 1 1 1 1 0 1 0 1 I can see *what* is going on, but what is the reason for it? I see another notebook entry coming :) See ?Arithmetic and read the paragraph under Details starting 'Logical vectors' Chuck; Compare these three, all of which are using binary operators on logical vectors which is what is being discussed in ?Arithmetic: duplicated(c(a, a, b) ) + 0 [1] 0 1 0 !duplicated(c(a, a, b) ) + 0 [1] TRUE FALSE TRUE 0 + !duplicated(c(a, a, b) ) [1] 1 0 1 I believe the proper place to go is ?Syntax where operator precedence is discussed. I think the precendence rules implicitly do this in the second instance, because + has higher precendence than negation: ! ( duplicated(c(a, a, b) ) + 0 ) David, Thanks. Both you and David Lorenz are correct in pointing to operator precedence as the answer to Dennis' question. Mea culpa for my not reading Dennis' question carefully enough to understand what his question really was! Best, Chuck -- David. Chuck Dennis On Tue, Nov 23, 2010 at 6:12 AM, David Winsemius dwinsem...@comcast.netwrote: On Nov 23, 2010, at 8:33 AM, Joel wrote: Is there any similar function in R to the first. in SAS? What it dose is: Lets say we have this table: a b c 1 1 5 1 0 2 2 0 2 2 0 NA 2 9 2 3 1 3 and then I want do to do one thing the first time the number 1 appers in a and something else the secund time 1 appers in a and so on. so something similar to: if first.a { a$d-1 }else{ a$d-0 } The duplicated function which returns a logical vector with those features can easily be coerced to numeric. df$d - as.numeric(!duplicated(df$a)) I was a bit puzzled about my failure to get coercion by the method which I thought was supposed to work, namely adding 0. df$e - !duplicated(df$a)+0 # does not coerce df$e - 0 + !duplicated(df$a) # pre-adding 0 does coerce Maybe the rules on coercion were amended. -- David This would give me a b c b 1 1 5 1 1 0 2 0 2 0 2 1 2 0 NA 0 2 9 2 0 3 1 3 1 Is there such a function in R or anything similar? thx //Joel -- View this message in context: http://r.789695.n4.nabble.com/the-first-from-SAS-in-R-tp3055417p3055417.html Sent from the R help mailing list archive at Nabble.com. __ 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. David Winsemius, MD West Hartford, CT __ 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]] __ 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. Charles C. BerryDept of Family/Preventive Medicine cbe...@tajo.ucsd.eduUC San Diego http://famprevmed.ucsd.edu/faculty/cberry/ La Jolla, San Diego 92093-0901 David Winsemius, MD West Hartford, CT Charles C. BerryDept of Family/Preventive Medicine cbe...@tajo.ucsd.eduUC San Diego http://famprevmed.ucsd.edu/faculty/cberry/ La Jolla, San Diego 92093-0901 __ 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.
Re: [R] the first. from SAS in R
Is there any similar function in R to the first. in SAS? ?duplicated a$d - ifelse( duplicated( a$a ), 0 , 1 ) a$d.2 - as.numeric( !duplicated( a$a ) ) Actually, duplicated does not duplicate SAS' first. operator, though it may suffice for the OP's needs. To illustrate, let's start with a dataframe of 3 key columns and some data in x: tt - data.frame(k1 = rep(1:3, each=10), k2 = rep(1:5, each=2, times=3), k3=rep(1:2, times=15), x = 1:30) # Try to mimic what the following SAS datastep would do, # assuming 'tt' is already sorted: # data foo; # set tt; # by k1, k2; # put first.k1=, first.k2=; # run; # SAS' first. operations would result in these values: tt$sas.first.k1 - rep(c(1, rep(0,9)), 3) tt$sas.first.k2 - rep(1:0, 15) # R duplicated() returns these values. You can see they # are the same for k1, but dissimilar after row 10 for k2. tt$duplicated.k1 - 0+!duplicated(tt$k1) tt$duplicated.k2 - 0+!duplicated(tt$k2) # I've found I need to lag a column to mimic SAS' first. # operator, thusly, though perhaps someone else knows # differently. Note this does not work on unordered # dataframes! lag.k1 - c(NA, tt$k1[1:(nrow(tt) - 1)]) tt$r.first.k1 - ifelse(is.na(lag.k1), 1, tt$k1 != lag.k1) lag.k2 - c(NA, tt$k2[1:(nrow(tt) - 1)]) tt$r.first.k2 - ifelse(is.na(lag.k2), 1, tt$k2 != lag.k2) Mimicking SAS' last. operation can be done in a similar manner, by anti-laging the column of interest and changing the comparisons somewhat. Enjoy the days, cur -- Curt Seeliger, Data Ranger Raytheon Information Services - Contractor to ORD seeliger.c...@epa.gov 541/754-4638 [[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.
Re: [R] the first. from SAS in R
On Tue, 23 Nov 2010, seeliger.c...@epamail.epa.gov wrote: Is there any similar function in R to the first. in SAS? ?duplicated a$d - ifelse( duplicated( a$a ), 0 , 1 ) a$d.2 - as.numeric( !duplicated( a$a ) ) Actually, duplicated does not duplicate SAS' first. operator, though it may suffice for the OP's needs. To illustrate, let's start with a dataframe of 3 key columns and some data in x: tt - data.frame(k1 = rep(1:3, each=10), k2 = rep(1:5, each=2, times=3), k3=rep(1:2, times=15), x = 1:30) # Try to mimic what the following SAS datastep would do, # assuming 'tt' is already sorted: # data foo; # set tt; # by k1, k2; # put first.k1=, first.k2=; # run; # SAS' first. operations would result in these values: tt$sas.first.k1 - rep(c(1, rep(0,9)), 3) tt$sas.first.k2 - rep(1:0, 15) # R duplicated() returns these values. You can see they # are the same for k1, but dissimilar after row 10 for k2. tt$duplicated.k1 - 0+!duplicated(tt$k1) tt$duplicated.k2 - 0+!duplicated(tt$k2) It depends on how you use duplicated() all.equal( tt$sas.first.k2, 0+!duplicated( tt[, c(k1,k2) ] ) ) [1] TRUE Chuck # I've found I need to lag a column to mimic SAS' first. # operator, thusly, though perhaps someone else knows # differently. Note this does not work on unordered # dataframes! lag.k1 - c(NA, tt$k1[1:(nrow(tt) - 1)]) tt$r.first.k1 - ifelse(is.na(lag.k1), 1, tt$k1 != lag.k1) lag.k2 - c(NA, tt$k2[1:(nrow(tt) - 1)]) tt$r.first.k2 - ifelse(is.na(lag.k2), 1, tt$k2 != lag.k2) Mimicking SAS' last. operation can be done in a similar manner, by anti-laging the column of interest and changing the comparisons somewhat. Enjoy the days, cur -- Curt Seeliger, Data Ranger Raytheon Information Services - Contractor to ORD seeliger.c...@epa.gov 541/754-4638 [[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. Charles C. BerryDept of Family/Preventive Medicine cbe...@tajo.ucsd.eduUC San Diego http://famprevmed.ucsd.edu/faculty/cberry/ La Jolla, San Diego 92093-0901 __ 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.
Re: [R] the first. from SAS in R
... # I've found I need to lag a column to mimic SAS' first. # operator, thusly, though perhaps someone else knows # differently. Note this does not work on unordered # dataframes! lag.k1 - c(NA, tt$k1[1:(nrow(tt) - 1)]) tt$r.first.k1 - ifelse(is.na(lag.k1), 1, tt$k1 != lag.k1) lag.k2 - c(NA, tt$k2[1:(nrow(tt) - 1)]) tt$r.first.k2 - ifelse(is.na(lag.k2), 1, tt$k2 != lag.k2) It depends on how you use duplicated() all.equal( tt$sas.first.k2, 0+!duplicated( tt[, c(k1,k2) ] ) ) [1] TRUE Wonderful. Concise. Good job. cur -- Curt Seeliger, Data Ranger Raytheon Information Services - Contractor to ORD seeliger.c...@epa.gov 541/754-4638 [[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.
Re: [R] the first. from SAS in R
I often use code like Curt's encapsulated in the following isFirstInRun function: isFirstInRun - function(x,...) { lengthX - length(x) if (lengthX == 0) return(logical(0)) retVal - c(TRUE, x[-1]!=x[-lengthX]) for(arg in list(...)) { stopifnot(lengthX == length(arg)) retVal - retVal | c(TRUE, arg[-1]!=arg[-lengthX]) } if (any(missing-is.na(retVal))) # match rle: NA!=NA retVal[missing] - TRUE retVal } E.g., d - data.frame(log=trunc(log(1:10)), sqrt=trunc(sqrt(1:10))) within(d, first - isFirstInRun(log)) log sqrt first 101 TRUE 201 FALSE 311 TRUE 412 FALSE 512 FALSE 612 FALSE 712 FALSE 822 TRUE 923 FALSE 10 23 FALSE # Or look for change in any number of vectors: within(d, first - isFirstInRun(log, sqrt)) # TRUE if either changes log sqrt first 101 TRUE 201 FALSE 311 TRUE 412 TRUE 512 FALSE 612 FALSE 712 FALSE 822 TRUE 923 TRUE 10 23 FALSE To do isLastInRun put the TRUE after the x[-1]!=x[-length(x)] isLastInRun - function(x,...) { lengthX - length(x) if (lengthX == 0) return(logical(0)) retVal - c(x[-1]!=x[-lengthX], TRUE) for(arg in list(...)) { stopifnot(lengthX == length(arg)) retVal - retVal | c(arg[-1]!=arg[-lengthX], TRUE) } if (any(missing-is.na(retVal))) # match rle: NA!=NA retVal[missing] - TRUE retVal } Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of seeliger.c...@epamail.epa.gov Sent: Tuesday, November 23, 2010 10:38 AM To: r-help@r-project.org Cc: Joel Subject: Re: [R] the first. from SAS in R Is there any similar function in R to the first. in SAS? ?duplicated a$d - ifelse( duplicated( a$a ), 0 , 1 ) a$d.2 - as.numeric( !duplicated( a$a ) ) Actually, duplicated does not duplicate SAS' first. operator, though it may suffice for the OP's needs. To illustrate, let's start with a dataframe of 3 key columns and some data in x: tt - data.frame(k1 = rep(1:3, each=10), k2 = rep(1:5, each=2, times=3), k3=rep(1:2, times=15), x = 1:30) # Try to mimic what the following SAS datastep would do, # assuming 'tt' is already sorted: # data foo; # set tt; # by k1, k2; # put first.k1=, first.k2=; # run; # SAS' first. operations would result in these values: tt$sas.first.k1 - rep(c(1, rep(0,9)), 3) tt$sas.first.k2 - rep(1:0, 15) # R duplicated() returns these values. You can see they # are the same for k1, but dissimilar after row 10 for k2. tt$duplicated.k1 - 0+!duplicated(tt$k1) tt$duplicated.k2 - 0+!duplicated(tt$k2) # I've found I need to lag a column to mimic SAS' first. # operator, thusly, though perhaps someone else knows # differently. Note this does not work on unordered # dataframes! lag.k1 - c(NA, tt$k1[1:(nrow(tt) - 1)]) tt$r.first.k1 - ifelse(is.na(lag.k1), 1, tt$k1 != lag.k1) lag.k2 - c(NA, tt$k2[1:(nrow(tt) - 1)]) tt$r.first.k2 - ifelse(is.na(lag.k2), 1, tt$k2 != lag.k2) Mimicking SAS' last. operation can be done in a similar manner, by anti-laging the column of interest and changing the comparisons somewhat. Enjoy the days, cur -- Curt Seeliger, Data Ranger Raytheon Information Services - Contractor to ORD seeliger.c...@epa.gov 541/754-4638 [[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. __ 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.