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

PIKAL Petr petr.pikal at precheza.cz
Mon Aug 26 15:48:50 CEST 2013


Hi

better approach is to use match. Let have some lookup table called test

> test
     one    two
1 Pst 24 -17.29
2 Pst 48 -43.93
3 Pto 24 -29.39

and original data called big

> big<-data.frame(one=sample(test$one, 20, replace=TRUE), two=100)
> big
      one two
1  Pto 24 100
2  Pst 48 100
3  Pst 24 100
4  Pst 48 100
5  Pst 48 100
6  Pto 24 100
7  Pto 24 100
8  Pst 24 100
9  Pst 24 100
10 Pst 48 100
11 Pst 24 100
12 Pto 24 100
13 Pto 24 100
14 Pst 24 100
15 Pto 24 100
16 Pst 48 100
17 Pst 48 100
18 Pst 24 100
19 Pto 24 100
20 Pst 48 100

Then
> big$two + test$two[match(big$one,test$one)]
 [1] 70.61 56.07 82.71 56.07 56.07 70.61 70.61 82.71 82.71 56.07 82.71 70.61
[13] 70.61 82.71 70.61 56.07 56.07 82.71 70.61 56.07

computes required normalisation in one step.

Regards
Petr


> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of arun
> Sent: Monday, August 26, 2013 8:32 AM
> To: Fethe, Michael
> Cc: R help
> Subject: Re: [R] If else loop problem: the condition has length > 1 and
> only the first element will be used
> 
> 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.



More information about the R-help mailing list