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. Berry Dept of Family/ Preventive Medicine
cbe...@tajo.ucsd.edu                        UC 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.

Reply via email to