[R] Creating an new variable -or my failure to understand an IF() statement

Chuck Cleland ccleland at optonline.net
Tue Apr 11 18:02:04 CEST 2006


John Kane wrote:
> I am just starting with R and I am having a stupid 
> problem understanding  an if else statement because I
> am thinking in terms of something like SAS or Fortran.
>  
> 
> I am completely misunderstanding what I am reading in
> the Intro to R,  the R-Language Definitions and the
> Help Mailing List.  I have read references to if not a
> vector and ifelse vectored and am even more confused.
> 
> Problem :   I want to create some new variables to use
> in a data.frame.  The actual data is read into
> data.frame (mixed string and numeric data) using a
> read.csv() command.
> 
> Testdata is:
>  a <- c("A", "B","C","D","E")
>   Y1 <- c(2, 2 , 400, 500, 600)
>   Y2 <- c(2, 4, 4 , 600, 700)
>   Y3 <- c(5, 4, 1, 1, 200)
>   Y4 <- c(5, 5, 1, 3,  5)
> 
>   MyData <- data.frame(a,Y1,Y2,Y3, Y4)
> 
>  MyData
>  # Results are:
>   a  Y1  Y2  Y3 Y4
> 1 A   2   2   5  5
> 2 B   2   4   4  5
> 3 C 400   4   1  1
> 4 D 500 600   1  3
> 5 E 600 700 200  5
> 
> 
> What I want to do is to add a variable to the
> data.frame so that I have:
> 
>    a  Y1  Y2   Y3 Y4   P1
> 1 A    2   2   5   5    4
> 2 B    2   4   4   5    8
> 3 C  400   4   1   1    2
> 4 D  500 600   1   3    4
> 5 E  600 700 200   5    NA
> 
> However when I try this :
> 
>  if (a=="A") (P1 <- Y1+ Y2) else
>    if (a=="B") (P1 <- Y2+Y3) else
>      if (a=="C" ) (P1 <- Y3+ Y4) else
>        if (a=="D" (P1 <-  Y4) else
>         if (a=="D") (P1<- NA)
> 
> Error: syntax error in:
> "     if (a=="C" ) (P1 <- Y3+ Y4) else
>        if (a=="D" (P1 <-  Y4) else"
>>         if (a=="D") (P1<- NA)
> Warning message:
> the condition has length > 1 and only the first
> element will be used in: if (a == "D") (P1 <- NA) 
>  
> Can anyone help me a) get around it and b) understand
> what R is really doing.?

MyData$P1 <- ifelse(a=="A", Y1 + Y2,
               ifelse(a=="B", Y2 + Y3,
                ifelse(a=="C", Y3 + Y4,
                 ifelse(a=="D", Y4, NA))))

?ifelse

> Thanks
> 
> John Kane, Kingston ON  Canada
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
> 

-- 
Chuck Cleland, Ph.D.
NDRI, Inc.
71 West 23rd Street, 8th floor
New York, NY 10010
tel: (212) 845-4495 (Tu, Th)
tel: (732) 512-0171 (M, W, F)
fax: (917) 438-0894




More information about the R-help mailing list