0

I am a complete newbie to both R and programming. Please forgive stupidity, but I'm assuming this will be an easy question to answer. I have a table (as below) and am trying to extract a set of values to add to a list. My attempt is as follows:

  cfemale0=(0)
  for (i in Gender) {
     if (Gender[i]=="F") {
       if (Treatment[i]=="Control") {
           cfemale0=c(cfemale0,Distance[i])
       }
     }
  }

Error in if (Gender[i] == "F") { : missing value where TRUE/FALSE needed

Here is an extract of the (very long) table:

14  1   F   Control 1   2
14  1   F   Control 2   1
14  1   F   Control 3   1
14  1   F   Control 4   1
14  1   F   Control 5   1
14  1   F   Control 6   2
14  1   F   Control 7   2
14  1   F   Control 8   1
14  1   F   Control 9   1
14  1   F   Control 10  1
14  1   F   Control 11  1
14  1   F   Control 12  1
14  1   F   Control 13  1
14  1   F   Control 14  13
14  1   F   Control 15  1
14  1   F   Control 16  0
14  1   F   MBR 1   1
14  1   F   MBR 2   2
14  1   F   MBR 3   18
14  1   F   MBR 4   5
14  1   F   MBR 5   16
14  1   F   MBR 6   5
14  1   F   MBR 7   0
14  1   F   MBR 8   1
14  1   F   MBR 9   3
14  1   F   MBR 10  1
14  1   F   MBR 11  17
14  1   F   MBR 12  16
14  1   F   MBR 13  17
14  1   F   MBR 14  16
14  1   F   MBR 15  17
14  1   F   MBR 16  2
14  1   F   MBL 1   2
14  1   F   MBL 2   2
14  1   F   MBL 3   1
14  1   F   MBL 4   2
14  1   F   MBL 5   1
14  1   F   MBL 6   3
14  1   F   MBL 7   4
14  1   F   MBL 8   3
14  1   F   MBL 9   3
14  1   F   MBL 10  3
14  1   F   MBL 11  2
14  1   F   MBL 12  2
14  1   F   MBL 13  2
14  1   F   MBL 14  1
14  1   F   MBL 15  1
14  1   F   MBL 16  1
12  2   M   Control 1   7
12  2   M   Control 2   7
12  2   M   Control 3   15
12  2   M   Control 4   19
12  2   M   Control 5   16
12  2   M   Control 6   19
12  2   M   Control 7   15
12  2   M   Control 8   18
12  2   M   Control 9   13
12  2   M   Control 10  19
12  2   M   Control 11  12
12  2   M   Control 12  4
12  2   M   Control 13  15
12  2   M   Control 14  14
12  2   M   Control 15  19
12  2   M   Control 16  6
12  2   M   MBR 1   7
12  2   M   MBR 2   15
12  2   M   MBR 3   15
12  2   M   MBR 4   3
12  2   M   MBR 5   8
12  2   M   MBR 6   10
12  2   M   MBR 7   17
12  2   M   MBR 8   3
davide
  • 1,918
  • 2
  • 20
  • 30
Tim Foster
  • 103
  • 4
  • 1
    http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – rmuc8 Mar 13 '15 at 12:11
  • 2
    try `for (i in seq_along(Gender))` ... – Ben Bolker Mar 13 '15 at 12:12
  • 3
    There is probably a straight-forward way to do this, but you don't provide enough information (no reproducible example). Right now you are in the second circle of the [R inferno](http://www.burns-stat.com/pages/Tutor/R_inferno.pdf). – Roland Mar 13 '15 at 12:14
  • 1
    When you are doing `i in Gender`, `i` is getting the values within `Gender` , thus when doing `Gender[i]=="F"` you are basically doing something like `"F"["F"] == "F"` or `"M"["F"] == "F"` which is invalid syntex, same for `Treament`. See @BenBolker suggestion on how to solve this. Either way, you shouldn't use a loop here at all, so @Rolands comment will come in handy. – David Arenburg Mar 13 '15 at 12:14
  • 1
    Tim Foster and [davide](http://stackoverflow.com/users/2357483/davide) same SO account? edit was suggested by davide – zx8754 Mar 13 '15 at 12:18
  • Cheers @BenBolker, that gets rid of my error message, but it doesn't add the appropriate values in Distance[i] to the list cfemale0 – Tim Foster Mar 13 '15 at 12:19
  • 1
    Loop is absolutely not necessary (and even more it should be avoided at all costs). Try simple subsetting as in `df[df$Gender == "F" & Treatment == "Control",]` – statespace Mar 13 '15 at 12:22
  • Thank you @DavidArenburg! That makes complete sense, changing it to for (i in 1:768) (i.e. the number of rows in the table) sorted it. – Tim Foster Mar 13 '15 at 12:24

2 Answers2

0

When you do for i in Gender, i will take the values of the elements of Gender. It is not a valid index (unless by chance), and you would usually get index out of bound error. You don't here probably because Gender is a list.

You get the error missing value where TRUE/FALSE needed when comparing performing and if statement on an NA value (i.e. doing if(NA)).

So, your solution is to provide a correct index to your Gender:

for (i in 1:length(Gender)) {
    if (Gender[i]=="F") {}
}

and you would change length to nrow or ncol if Gender was a matrix.

Math
  • 2,399
  • 2
  • 20
  • 22
0

I can only guess the structure of your data, but if it was in data frame - all you'd have to do is:

vector <- df$Distance[df$Gender == "F" & df$Treatment == "Control",]
statespace
  • 1,644
  • 17
  • 25