Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using If/Else on a data frame

Tags:

r

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

but no one has answered that question.

Many thanks in advance!

like image 824
Timothy Alston Avatar asked Aug 08 '12 13:08

Timothy Alston


People also ask

Can we use if else in pandas DataFrame?

pandas is a Python library built to work with relational data at scale. As you work with values captured in pandas Series and DataFrames, you can use if-else statements and their logical structure to categorize and manipulate your data to reveal new insights.


2 Answers

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

like image 146
Andrie Avatar answered Oct 06 '22 03:10

Andrie


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 
like image 40
Jilber Urbina Avatar answered Oct 06 '22 04:10

Jilber Urbina