[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 14:45:59 CEST 2013
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.
More information about the R-help
mailing list