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

Leeds, Mark (IED) Mark.Leeds at morganstanley.com
Wed Oct 10 22:01:58 CEST 2007


temp <- data.frame(x=rnorm(20), y=rnorm(20)
signdf<-lapply(temp, function(.col) ifelse( .col < 0, -1, 1))

But I'm not sure how to take those two lists and make a data frame back
out of them.
Any help is still appreciated because this is only a partial solution.
 

-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
On Behalf Of Georg Ehret
Sent: Wednesday, October 10, 2007 3:43 PM
To: r-help at stat.math.ethz.ch
Subject: [R] simple function with if -> lapply to dataframe

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?

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.
--------------------------------------------------------

This is not an offer (or solicitation of an offer) to bu...{{dropped:22}}



More information about the R-help mailing list