[R] If else loop problem: the condition has length > 1 and only the first element will be used

arun smartpink111 at yahoo.com
Tue Aug 27 02:48:24 CEST 2013


Hi,
Your attachment didn't came through..
Using the same example, that I used before:
set.seed(549)
 dat1<- data.frame(t.tr=sample(c(paste("Pst", c(24, 48, 72)), paste("Pto", c(24, 48, 72)), paste ("Pm", c(24, 48, 72))), 50, replace=TRUE), signal2 = sample(600:700, 50, replace=TRUE))

dat2<- data.frame(t.tr=c(paste("Pst", c(24, 48, 72)), paste("Pto", c(24, 48, 72)), paste ("Pm", c(24, 48, 72))),val=c(-17.29, -43.93, -43.47, -29.39, -76.79, -68.17, 0.49, -0.20, -10.46))
library(plyr)
res<-within(join(dat1,dat2,by="t.tr"),{signal3<-signal2+val})
 with(dat1,ifelse(t.tr=="Pst 24",signal2 - 17.29,
    ifelse(t.tr=="Pst 48",signal2 - 43.93256,
     ifelse(t.tr=="Pst 72",signal2 - 43.477468,
     ifelse(t.tr=="Pto 24",signal2 - 29.39875,
     ifelse(t.tr=="Pto 48",signal2 - 76.796645,
     ifelse(t.tr=="Pto 72",signal2 - 68.176174,
     ifelse(t.tr=="Pm 24",signal2 + 0.498333,
     ifelse(t.tr=="Pm 48",signal2 - 0.200417,
     ifelse(t.tr=="Pm 72",signal2 - 10.465507,
     signal2))))))))))
# [1] 628.6013 607.7996 645.5225 555.8238 597.8238 590.6013 594.0674 654.0674
 #[9] 663.7996 608.7100 580.8238 594.8238 611.0674 607.8238 615.7996 622.7996
#[17] 623.2034 607.6013 628.5345 573.6013 566.2034 565.0674 558.8238 620.7100
#[25] 583.0674 605.6013 616.7996 582.8238 627.4983 664.7100 606.0674 613.7996
#[33] 553.2034 636.0674 655.5345 550.2034 544.2034 638.6013 662.7996 606.7100
#[41] 595.5345 610.6013 653.4983 648.7100 625.6013 611.5345 599.8238 604.7100
#[49] 577.6013 659.7100

 res[,4]
# [1] 628.61 607.80 645.53 555.83 597.83 590.61 594.07 654.07 663.80 608.71
#[11] 580.83 594.83 611.07 607.83 615.80 622.80 623.21 607.61 628.54 573.61
#[21] 566.21 565.07 558.83 620.71 583.07 605.61 616.80 582.83 627.49 664.71
#[31] 606.07 613.80 553.21 636.07 655.54 550.21 544.21 638.61 662.80 606.71
#[41] 595.54 610.61 653.49 648.71 625.61 611.54 599.83 604.71 577.61 659.71

I am using R 3.0.1.  May be you try by:

dat1$t.tr<- as.character(dat1$t.tr)
A.K.






----- Original Message -----
From: "Fethe, Michael" <mfethe1 at utk.edu>
To: Dennis Murphy <djmuser at gmail.com>; "R-help at r-project.org" <R-help at r-project.org>
Cc: 
Sent: Monday, August 26, 2013 8:45 AM
Subject: Re: [R] If else loop problem: the condition has length > 1 and only the first element will be used

Thank you all for your suggestions. However, Dennis since your method seemed the easiest. I gave it a shot with no success while it produces  NAs. The R inferno is not helpful for this part of my data normalization. I need to apply this to my whole data.frame before I perform statistical tests (ie. multiple comparisons, anova...). I have attached a subset to see if anything is the problem. As for the ifelse loops, these ifelse can contain other conditions within them? ie. 
ifelse(t.tr=="Pst 24",signal2-58,ifelse(t.tr=="Pst 48",signal3=signal2-67,ifelse(...))),signal3=signal2)
aren't these cumbersome compared to a simple if else loop. Would this logic work though?
This seems to produce more problems here is my code for the ifelse loops:

signal3=ifelse(t.tr=="Pst 24",signal2 - 17.29,
    ifelse(t.tr=="Pst 48",signal2 - 43.93256,
    ifelse(t.tr=="Pst 72",signal2 - 43.477468,
    ifelse(t.tr=="Pto 24",signal2 - 29.39875,
    ifelse(t.tr=="Pto 48",signal2 - 76.796645,
    ifelse(t.tr=="Pto 72",signal2 - 68.176174,
    ifelse(t.tr=="Pm 24",signal2 + 0.498333,
    ifelse(t.tr=="Pm 48",signal2 - 0.200417,
    ifelse(t.tr=="Pm 72",signal2 - 10.465507,
    signal2)))))))))
signal3

As far as i can tell my syntax is correct. Is there a way to overcome the signal2-x. Do i need to assign each signal2-x a value (ie. a-j)? This code produces : 
In Ops.factor(signal2, 10.465507) : - not meaningful for factors

Thanks for the input.
________________________________________
From: Dennis Murphy <djmuser at gmail.com>
Sent: Monday, August 26, 2013 4:59 AM
To: Fethe, Michael
Subject: Re: [R] If else loop problem: the condition has length > 1 and only the first element will be used

Hi:

Here's a possible workaround:

adj <- 17.29 * (t.tr == "Pst 24") + 43.93256 * (t.tr == "Pst 48") +
          43.477468 * (t.tr == "Pst 72") + ...

Then use

signal3 <- signal2 - adj

Dennis

On Sun, Aug 25, 2013 at 6:31 PM, Fethe, Michael <mfethe1 at utk.edu> wrote:
> Hi all,
>
> I'm running an if else loop to normalize my data to a known control.
> I have calculated values that need to be subtracted from each treatment applied. I'm trying this within a for loop with if and else commands to apply to correct subtraction. This is done as follows:
>
>
> attach(data.2013)
>
> #Normalize to known control
>
> for(i in signal2) {
>
> if(t.tr=="Pst 24") {signal3=(signal2 - 17.29)}
>
> if(t.tr=="Pst 48") {signal3=(signal2 - 43.93256)}
>
> if(t.tr=="Pst 72") {signal3=(signal2 - 43.477468)}
>
> if(t.tr=="Pto 24") {signal3=(signal2 - 29.39875)}
>
> if(t.tr=="Pto 48") {signal3=(signal2 - 76.796645)}
>
> if(t.tr=="Pto 72") {signal3=(signal2 - 68.176174)}
>
> if(t.tr=="Pm 24") {signal3=(signal2 + 0.498333)}
>
> if(t.tr=="Pm 48") {signal3=(signal2 - 0.200417)}
>
> if(t.tr=="Pm 72") {signal3=(signal2 - 10.465507)}
>
> else {signal3=signal2}
>
> }
>
>
> This code ran once with no problems or warnings. Now it produces a ton of warnings of
>
> "the condition has length > 1 and only the first element will be used".
>
>
> Here is str(data.2013)
>
>
> 'data.frame': 4293 obs. of  8 variables:
>
>  $ rep      : Factor w/ 4 levels "R1","R2","R3",..: 1 1 1 1 1 1 1 1 1 1 ...
>
>  $ construct: Factor w/ 10 levels "35S","46S","E1",..: 1 1 1 1 1 1 1 1 1 1 ...
>
>  $ time     : int  24 24 24 24 24 24 24 24 24 24 ...
>
>  $ treatment: Factor w/ 8 levels "Mock","MockUN",..: 1 1 1 1 1 1 2 2 2 3 ...
>
>  $ sample   : int  1 2 3 4 5 6 1 2 3 1 ...
>
>  $ signal   : int  733750 665790 659550 676270 682770 600420 676350 652110 665350 798850 ...
>
>  $ signal2  : num  734 666 660 676 683 ...
>
>  $ t.tr     : Factor w/ 24 levels "Mock 24","Mock 48",..: 1 1 1 1 1 1 4 4 4 7 ...
>
>
>
> I'm really not sure why this ran once and now is giving me errors.
>
> Can anyone help? I've heard ifelse can be effective when running into this error. However, this command does not function the way i would like. I need a specific value subtracted from each treatment/time value. The only value that should matter for subtracting the correct value is t.tr.
>
>
>
>         [[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.




______________________________________________
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.




More information about the R-help mailing list