You are working with a matrix, so the "$" operator is not allowed (e.g., d$c).
Also in your test, you have to test against the second column (e.g., d[i, 2]) try this: > a <- c(1:4) > b <- c("meep", "foo", "meep", "foo") > d <- cbind(a, b) > > > for(i in seq(along=d[,2])) {if (d[i,2]=="meep") { print("oops")} + else { print("yay")} + } [1] "oops" [1] "yay" [1] "oops" [1] "yay" > > # put results back > d <- cbind(d, c=ifelse(d[,2] == 'meep', 'oops', 'yay')) > d a b c [1,] "1" "meep" "oops" [2,] "2" "foo" "yay" [3,] "3" "meep" "oops" [4,] "4" "foo" "yay" > On Sun, Apr 18, 2010 at 8:46 AM, Laura Ferrero-Miliani <laur...@gmail.com>wrote: > Hello, > I am very new to R and data analysis in general. > I am trying to generate values to append to my data frame using > conditional statements. > I am playing with this simple example: > > a <- c(1:4) > b <- c("meep", "foo", "meep", "foo") > d <- cbind(a, b) > > now what I want to do is , each time there is a "meep" in column 2 of > d, print "oops", else print "yay". > So I wrote: > > for(i in seq(along=d[,2])) {if (d[i]=="meep") { print("oops")} > else { print("yay")} > } > > Result: > [1] "yay" > [1] "yay" > [1] "yay" > [1] "yay" > > What am I doing wrong? > > Furthermore, I would like to append the results to d: > > d$c <- for(i in seq(along=d[,2])) {if (d[i]=="meep") { print("oops")} > else { print("yay")} > } > > > this doesn't really work, it just turns the whole thing into a list. > . > Although if: > > c <- NA > d <- cbind(a, b, c) > > and I coerce d into a data.frame, run: > > d$c <- for(i in seq(along=d[,2])) {if (d[i]=="meep") { print("oops")} > else { print("yay")} > } > > > some glint of hope appears: > > > [1] "yay" > [1] "oops" > > but then...... > > > > Error in if (d[i] == "meep") { : missing value where TRUE/FALSE needed > In addition: Warning messages: > 1: In if (d[i] == "meep") { : > the condition has length > 1 and only the first element will be used > 2: In if (d[i] == "meep") { : > the condition has length > 1 and only the first element will be used > 3: In if (d[i] == "meep") { : > the condition has length > 1 and only the first element will be used > > > To complicate things a little bit more in my real data there are 16 > levels, so for each level I need to "print" a different value (that > would be 16 nested ifs, and I am sure there must be a more sensible > way to do this!) > > > Thanks in advance, > > Laura > > ______________________________________________ > R-help@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<http://www.r-project.org/posting-guide.html> > and provide commented, minimal, self-contained, reproducible code. > -- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve? [[alternative HTML version deleted]] ______________________________________________ R-help@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.