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

James W. MacDonald jmacdon at med.umich.edu
Wed Oct 10 22:03:42 CEST 2007


Hi Georg,

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?

The main problem here is you are ignoring existing functions that will 
do the job much better. See ?sign.

And the error you see is because you are doing a single comparison, but 
passing a vector for x (e.g., x<0 tests if one thing is less than zero, 
but you pass a vector, so you get x[1]<0).

Best,

Jim
> 
> Thanking you in advance,
> 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] -1
> 
> $meanSBP_tttcorr
> [1] -1
> 
> $res_asb_SBP
> [1] -1
> 
> $res_asb_SBP_tttcorr
> [1] -1
> 
> $meanDBP
> [1] 1
> 
> $meanDBP_tttcorr
> [1] 1
> 
> $res_asb_DBP
> [1] 1
> 
> $res_asb_DBP_tttcorr
> [1] 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
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

-- 
James W. MacDonald, M.S.
Biostatistician
Affymetrix and cDNA Microarray Core
University of Michigan Cancer Center
1500 E. Medical Center Drive
7410 CCGC
Ann Arbor MI 48109
734-647-5623



More information about the R-help mailing list