0

I am encountering something really strange.

I have a data frame called stand. I also have standards=1:2. If the number of rows of stand is less than or equal to 8, I just want to output 2:3, which should be standards+1. If it is greater than 8, I would like all of the columns being specified by the standards argument, except for the last one plus one. The standards vector refers to column labels that are offset by one from where they sit in the source data frame, hence the +1 alteration. Here is where it gets weird.

Here is exactly what I have entered

stand<-structure(list(Stand.Conc = c(5000, 2500, 1250, 625, 312.5, 0
)), .Names = "Stand.Conc", row.names = c(NA, -6L), class = "data.frame")
standards<-1:2

> nrow(stand)
[1] 6
> nrow(stand)>8
[1] FALSE
> 1+standards[1:(length(standards)-1)]
[1] 2
> standards+1
[1] 2 3
> ifelse(nrow(stand)>8,1+standards[1:(length(standards)-1)],standards+1)
[1] 2
> if(nrow(stand)>8){
+           1+standards[1:(length(standards)-1)]}else{
+               standards+1
+               }
[1] 2 3

Somebody please help me understand what is going on here since ifelse and if else are returning two different things. if then else is working right, but ifelse seems to be returning the TRUE value even if FALSE is the result of the test. I am using R 3.2.1 GUI 1.66 Mavericks build (6956) on a MAC version 10.10.4

Eich
  • 93
  • 5
  • 1
    Not so strange if you read the documentation for `ifelse`. Particularly the first sentence of the documentation. They are different functions for different purposes. – joran Sep 04 '15 at 22:09
  • Thanks for clearing that up. It was really helpful. That of course was sarcasm, but I see what happened now. I was using the book R In Action as my reference, and when I read the ifelse section, I don't recall seeing a substantial difference between format outputs of ifelse vs if, just that one is vectorized and one isn't – Eich Sep 04 '15 at 22:55
  • You're welcome! Always happy to help! :) – joran Sep 04 '15 at 23:39

0 Answers0