42

I have a data set which looks something like

data<-c(0,1,2,3,4,2,3,1,4,3,2,4,0,1,2,0,2,1,2,0,4)
frame<-as.data.frame(data)

I now want to create a new variable within this data frame. If the column "data" reports a number of 2 or more, I want it to have "2" in that row, and if there is a 1 or 0 (e.g. the first two observations), I want the new variable to have a "1" for that observation.

I am trying to do this using the following code:

frame$twohouses<- if (any(frame$data>=2)) {frame$twohouses=2} else {frame$twohouses=1}

However if I run these 3 lines of script, every observation in the column "twohouses" is coded with a 2. However a number of them should be coded with a 1.

So my question: what am I doing wrong with my if else line or script? Or is there an alternative way to do this.

My question is similar to this one: Using ifelse on factor in R

ut no one has answered that question.

starball
  • 20,030
  • 7
  • 43
  • 238
Timothy Alston
  • 1,501
  • 5
  • 18
  • 29

2 Answers2

68

Use ifelse:

frame$twohouses <- ifelse(frame$data>=2, 2, 1)
frame
   data twohouses
1     0         1
2     1         1
3     2         2
4     3         2
5     4         2
...
16    0         1
17    2         2
18    1         1
19    2         2
20    0         1
21    4         2

The difference between if and ifelse:

  • if is a control flow statement, taking a single logical value as an argument
  • ifelse is a vectorised function, taking vectors as all its arguments.

The help page for if, accessible via ?"if" will also point you to ?ifelse

Andrie
  • 176,377
  • 47
  • 447
  • 496
  • naturally it would turn out to be something frustratingly easy after spending ages fighting with R. Thanks! – Timothy Alston Aug 08 '12 at 13:14
  • @TimothyAlston I added some pointers on how to get help on `if` and `ifelse` – Andrie Aug 08 '12 at 13:19
  • Thanks for this. This worked well for a larger data problem that I was working on and ended up being more efficient than many of the other methods I was attempting. Simple, but compelling. – Nathaniel Payne May 06 '14 at 08:23
3

Try this

frame$twohouses <- ifelse(frame$data>1, 2, 1)
 frame
   data twohouses
1     0         1
2     1         1
3     2         2
4     3         2
5     4         2
6     2         2
7     3         2
8     1         1
9     4         2
10    3         2
11    2         2
12    4         2
13    0         1
14    1         1
15    2         2
16    0         1
17    2         2
18    1         1
19    2         2
20    0         1
21    4         2
Jilber Urbina
  • 58,147
  • 10
  • 114
  • 138