# [R] simple function with if -> lapply to dataframe

Julian Burgos jmburgos at u.washington.edu
Thu Oct 11 03:39:14 CEST 2007

```Hi Georg,

The answer is in the warning message
In if (x < 0) a <- -1 else a <- 1 :
the condition has length > 1 and only the first element will be used

Basically you are passing a vector of length>1 to a control flow
expression (if() in this case) that requires only a vector of length
one.  See  ?Control or ?"if"

By the way, there is already an R function that returns the sign of a
numeric vector of any length.  Its, of course, sign().

Julian

Georg Ehret wrote:
> Dear R,
>    I am writing a simple function to extract the sign of values and apply it
> to a data frame (see below). Whatever I do I get error-messages... What is
> wrong?
>
> Cheers, Georg.
> ***********************
> Georg Ehret
> Institute of Genetic Medicine
> Johns Hopkins University
> Baltimore
>
>
>> extractsign<-function(x){
> +             if (x<0) a<--1
> +             else a<-1
> +             return (a)
> +             }
>> a
>            meanSBP meanSBP_tttcorr res_asb_SBP res_asb_SBP_tttcorr meanDBP
> meanDBP_tttcorr res_asb_DBP res_asb_DBP_tttcorr
> one          -0.04           -0.01       -0.11               -0.05
> 0.18
> 0.24        0.18                0.24
> two           0.06            0.01        0.33                0.24
> 0.00
> -0.02        0.05                0.03
> three        -0.01            0.01       -0.07               -0.01
> -0.01
> 0.00       -0.05               -0.01
> four         -0.14           -0.05       -0.05                0.00
> -0.85
> -0.59       -0.79               -0.55
> five          0.12            0.06        0.17                0.07
> 0.34
> 0.21        0.60                0.44
> six          -0.06           -0.01       -0.05               -0.01
> -0.26
> -0.14       -0.41               -0.25
>> lapply(a,extractsign)
> \$meanSBP
>  -1
>
> \$meanSBP_tttcorr
>  -1
>
> \$res_asb_SBP
>  -1
>
> \$res_asb_SBP_tttcorr
>  -1
>
> \$meanDBP
>  1
>
> \$meanDBP_tttcorr
>  1
>
> \$res_asb_DBP
>  1
>
> \$res_asb_DBP_tttcorr
>  1
>
> Warning messages:
> 1: In if (x < 0) a <- -1 else a <- 1 :
>   the condition has length > 1 and only the first element will be used
> 2: In if (x < 0) a <- -1 else a <- 1 :
>   the condition has length > 1 and only the first element will be used
> 3: In if (x < 0) a <- -1 else a <- 1 :
>   the condition has length > 1 and only the first element will be used
> 4: In if (x < 0) a <- -1 else a <- 1 :
>   the condition has length > 1 and only the first element will be used
> 5: In if (x < 0) a <- -1 else a <- 1 :
>   the condition has length > 1 and only the first element will be used
> 6: In if (x < 0) a <- -1 else a <- 1 :
>   the condition has length > 1 and only the first element will be used
> 7: In if (x < 0) a <- -1 else a <- 1 :
>   the condition has length > 1 and only the first element will be used
> 8: In if (x < 0) a <- -1 else a <- 1 :
>   the condition has length > 1 and only the first element will be used
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help