mardi 18 juillet 2017

Create column with a certain condition in R

I have this data.

OPENING CLOSE 
2007     2008   
2009     2010    
2004      NA   

and I would like to make this column

OPENING CLOSE Y2004 Y2005 Y2006 Y2007 Y2008 Y2009 Y2010
2007     2008                     1     1
2005     2008         1     1     1     1                                   
2004      NA    1     1     1     1     1     1     1

It is possible to create this column step by step with if function, while I'd like to make loop or lapply function.

Additionally, I want to make this column(S~) using a certain condition.

If a column (Y2007) is 1 and the column 3 years ago is 1 (Y2005), The new column (S2007) is 1 and otherwise 0.

OPENING CLOSE Y2004 Y2005 Y2006 Y2007 Y2008 Y2009 Y2010 | S2007 S2008 S2009
2007     2008                     1     1               |   0     0     0
2005     2008         1     1     1     1               |   1     1     0
2004      NA    1     1     1     1     1     1     1   |   1     1     1

How do I make the script in R?

Aucun commentaire:

Enregistrer un commentaire