[R] using if then statements in a dataframe
Weidong Gu
anopheles123 at gmail.com
Thu Aug 11 03:28:12 CEST 2011
Mike,
One thing I noticed that '&&' should be replaced by '&'. The former is
applied to the first element of the vector while the latter applicable
to the whole vector.
HTH
Weidong Gu
On Wed, Aug 10, 2011 at 7:34 PM, Ward, Michael Patrick
<mpward at illinois.edu> wrote:
>
> I used this service several months ago and was very pleased with the response.
>
> I have a dataframe with several thousand lines and to each line I need to apply a series of "if else" statements. For each row I need either a value or a blank/NA. Below is the series of if else statements I have been trying without success to integrate into a function such as "apply".
>
> if (t1.secondstrongest<-13000) {NA} else
> if (noise>-13000) {NA} else
> if ((abs(value.str1-value.str2))==120) {NA} else
> if ((abs(value.str1-value.str2))==180) {NA} else
> if ((abs(value.str1-value.str2))==240) {NA} else
> if (value.str1 ==300&&value.str2 ==0) {value.str1_adj+ bearingdiff} else
> if (value.str1 ==0&&value.str2 ==360) {value.str1_adj+360 - bearingdiff} else
> if (value.str2<value.str1) {value.str1_adj-bearingdiff} else
> if (value.str2>value.str1) {value.str1_adj+bearingdiff}
>
>
> Below is an example of the dataframe
>
> t1.secondstrongest Ant_test value.str1 value.str1_adj value.str2 value.str2_adj Noise bearingdiff
> 1 -10682 60 240 242 180 182 -12344 11.23
> 2 -11154 240 0 2 240 242 -13444 27.23
> 3 -10930 300 0 2 300 302 -14022 27.55
> 4 -10997 240 300 302 60 62 -13456 14.23
> 5 -11244 180 180 182 0 2 -14209 25.22
> 6 -12744 60 180 182 240 242 -14134 8.13
>
> The answer to the examples should be...
>
> 1 NA
>
> 2 NA
>
> 3 334.45
>
> 4 NA
>
> 5 NA
>
> 6 190.13
>
>
> THANKS!
> Mike
> Department of Natural Resources and Environmental Sciences
> University of Illinois
>
> [[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