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

Bert Gunter gunter.berton at gene.com
Mon Aug 26 16:09:46 CEST 2013


Suggestion:

Don't do the ifelse stuff below.

See ?switch  instead.

-- Bert

On Sun, Aug 25, 2013 at 11:32 PM, arun <smartpink111 at yahoo.com> wrote:
> HI,
>
> It may be better to provide an example dataset using ?dput().
> dput(head(dataset),20)
>
> Try:
> signal3<- ifelse(t.tr=="Pst 24", signal2-17.29, ifelse(t.tr=="Pst 48", signal2 - 43.93256, etc.....))
>
>
> A.K.
>
> ----- Original Message -----
> From: "Fethe, Michael" <mfethe1 at utk.edu>
> To: "r-help at r-project.org" <r-help at r-project.org>
> Cc:
> Sent: Sunday, August 25, 2013 9:31 PM
> Subject: [R] If else loop problem: the condition has length > 1 and only the first element will be used
>
> 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.



-- 

Bert Gunter
Genentech Nonclinical Biostatistics

Internal Contact Info:
Phone: 467-7374
Website:
http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm



More information about the R-help mailing list