[R] Creating an new variable -or my failure to understand an IF() statement
John Kane
jrkrideau at yahoo.ca
Tue Apr 11 17:24:41 CEST 2006
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.?
Thanks
John Kane, Kingston ON Canada
More information about the R-help
mailing list