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

Fethe, Michael mfethe1 at utk.edu
Mon Aug 26 17:59:26 CEST 2013


Match worker perfectly. Thanks for the help!

Michael Fethe

On Aug 26, 2013, at 11:10 AM, "arun" <smartpink111 at yahoo.com> wrote:

> Hi,
> 
> You could try:
>  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})
>  head(res)
> #    t.tr signal2    val signal3
> #1 Pto 24     658 -29.39  628.61
> #2  Pm 48     608  -0.20  607.80
> #3 Pst 72     689 -43.47  645.53
> #4 Pto 72     624 -68.17  555.83
> #5 Pto 72     666 -68.17  597.83
> #6 Pto 24     620 -29.39  590.61
> 
> 
> #or ?merge()
> 
> A.K.
> 
> 
> 
> 
> ----- Original Message -----
> From: Bert Gunter <gunter.berton at gene.com>
> To: arun <smartpink111 at yahoo.com>
> Cc: "Fethe, Michael" <mfethe1 at utk.edu>; R help <r-help at r-project.org>
> Sent: Monday, August 26, 2013 10:09 AM
> Subject: Re: [R] If else loop problem: the condition has length > 1 and only the first element will be used
> 
> 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