« first day (1246 days earlier)      last day (2055 days later) » 

6:28 AM
good morning!
 
6:43 AM
Hello.
 
Hi all :-)
 
6:58 AM
good morning all
 
7:21 AM
Can this be duped by this? It is the non-negate version
 
@Sotos hammered
 
cheerz @Ronak
 
8:32 AM
Oh this is interesting
x <- 1
length(x) <- 2
if (length(x) <- 2) {
  print("Done")
}
Why does it evaluate to TRUE?
(based on this comment)
(length(x) <- 2) == TRUE is FALSE. I'm missing something.
 
> x
[1] 1 NA
apperently 'x' gets extended with NA's
@RomanLuštrik does this help:
> 2 == TRUE
[1] FALSE
> 1 == TRUE
[1] TRUE
 
You are probably right. It just checks the first element and ignores the rest.
 
2 is not equal to TRUE; so, when you assign 2 as length of 'x' and compare that to TRUE it will return FALSE
 
8:56 AM
OK, so how do we explain the evaluation to TRUe in the if statement?
It would seem assigning a value to a variable inside an if() statement evaluates to TRUE.
    if (x <- 1) {
      print("Done")
    }

    [1] "Done"
 
Hi
@RomanLuštrik o_O interesting.
 
Somewhere somehow a 1 or TRUE is returned and passed to if.
 
Maybe this explains it, as <- is a function:
as.logical(sum(33))
# [1] TRUE
as.logical(sum())
# [1] FALSE
 
@RomanLuštrik Well <- does return what it did, so it boils down to if (1)
 
> as.logical(x <- 1)
[1] TRUE
And even
> as.logical(x <- 2)
[1] TRUE
 
9:04 AM
as.logical(x <- 0) is false :)
so IF is checking value of x
 
as.logical will return true for any value not 0
 
Mystery solved. dusts hands
 
if (x<-0) {
 print('ok')
} else {
  print('nok')
}
[1] "nok"
@JeroenE We have strict rules about write access to this room which your profile doesn't match, sorry, see davidarenburg.github.io/GMTs for more details
 
@RomanLuštrik still not clear to me, try:
x <- 33; if(length(x) <- 2) {"yes"} else {"no"}
x <- 33; length(x) <- 2; if(x) {"yes"} else {"no"}

x <- 0; if(length(x) <- 2) {"yes"} else {"no"}
x <- 0; length(x) <- 2; if(x) {"yes"} else {"no"}
Not sure how "length(x) <- 2" inside if gets evaluated.
 
@zx8754 because it is possible to assign a length of 2 to 'x'? (where possible equals to TRUE)
another example which might illustrate that:
> x <- 33; if(length(x) <- "a") {"yes"} else {"no"}
Error in length(x) <- "a" : vector size cannot be NA/NaN
In addition: Warning message:
In length(x) <- "a" : NAs introduced by coercion
 
9:15 AM
@Jaap Mystery solved. dusts hands, thanks
 
> print(length(x) <- 2)
[1] 2
from <- doc in return value:
> value. Thus one can use a <- b <- c <- 6.
All comes from that in fact
 
close as RTFM. :D
 
That's a well know use for golfing
Usually when you want to use something in a function call and save it for later
Exemple:
x=ncol(df)
y=nrow(df)
m=x*y
can be done as m=x<-ncol(df)*y<-nrow(df) and use the same amount of chars (on more complex scenarios you can save chars using this trick)
 
I get an error for this code
Error in ncol(df) * y <- nrow(df) : could not find function "*<-"
 
9:35 AM
Missing ;?
 
@Tensibai wrong doc, should be ?`length<-` innit, though the conclusion is the same
@germcd needs parentheses since it currently evals as m <- x <- expr*b <- y <- expr2, but everything except the final term should be a symbol to assign to, not an expression. should be m <- (x <- expr)*(y <- expr2)
btw, hullo all
 
10:02 AM
@Frank Thanks, that's what I was missing.
 
@Frank indeed
 
omg 400 upvotes for paste, should be closed as rtfm stackoverflow.com/q/7201341/680068
 
I usually use it more in things like cat(x<-scan()) to read, assign and show the value at the same time
 
@zx8754 tag with ?
 
@Cath k
 
10:21 AM
@zx8754 as we're retagging this question, not sure is useful here, there is already ... ?
 
10:42 AM
@zx8754 It's a 2011 answer. Not sure if we should really be digging up graves...
Also, upvote may not reflect just answer quality, but also how useful it was to the reader.
And this must have come up many times for users to upvote it...
 

« first day (1246 days earlier)      last day (2055 days later) »