0

I want to create two variables vector: first one is a crescent number went meet some conditions. In Excel, I have this formula

year1=IF(O(AM57=1;AO57=1;AQ57=1;AS57=1);0;1)
year2=IF(AT57=1;1;IF(B57=$C$50;1;B57+1))
year3=IF(AT58=1;1;IF(B58=$C$50;1;B58+1))
year4=IF(AT59=1;1;IF(B59=$C$50;1;B59+1))

. . .

But I have n=1445 and years for n are 9465 rows

Another variable has

ageid1=NORMINV(RANDOM();C$55;C$54)
ageid1=IF(OR($AT57=1;$B57=$C$50);NORMINV(RANDOM();C$55;C$54);C57+1)
ageid1=IF(O($AT58=1;$B58=$C$50);NORMINV(RANDOM();C$55;C$54);C58+1)
ageid1=IF(O($AT59=1;$B59=$C$50);NORMINV(RANDOM();C$55;C$54);C59+1)
ageid2==IF(O($AT65=1;$B65=$C$50);NORMINV(RANDOM();C$55;C$54);C65+1)

. . .

Please some help me to translate this kind of conditions.

Thank you.

PD. Thanks @jared-mamrot

Marcelo
  • 45
  • 4
  • 1
    You can use `ifelse()`, e.g. `ifelse(AT57 == 1, 1, ifelse(B57 == C50, 1, B57 + 1))` but instead of referencing cells, you reference values, e.g. if you have a dataframe called "df" you could refer to specific 'cells' by their index: `ifelse(df[2,3] == 1, 1, ifelse(df[4,5] == df[2,2], 1, df[7,8] + 1))`. See https://campus.datacamp.com/courses/model-a-quantitative-trading-strategy-in-r/chapter-1-introduction-to-r-for-trading?ex=4 for more details. Also, please see https://stackoverflow.com/help/how-to-ask / https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – jared_mamrot May 10 '21 at 02:40
  • Using `ifelse` on a single cell of a frame is the wrong approach. The question is unclear at the moment if you're concerned with just a single cell or if you intend to do this on columns or rows of a table. Marcelo, please [edit] your question and provide usable sample data by pasting the output of `dput(x)` where `x` is a small, representative sample of whatever R data you are using. – r2evans May 10 '21 at 02:50
  • @r2evans thank you, but I don't have a data set, because I try to make a df whit conditionals values and follow the same structure as excel (the Excel is a mess) – Marcelo May 10 '21 at 03:33
  • 1
    I don't understand. If you have data in Excel and want to do something with it in R, then that is your dataset. Start with the raw data (no excel formulas) and define what you need to do to mimic what is being done in Excel. – r2evans May 10 '21 at 03:38
  • Of course, I have already passed many columns to R to define how you indicate, but to create those last variables I do not know how to call the condition so that a number grows when it reaches some conditions. For example, the age of one of the people who begins in the sample is 20, throughout 29 years in the study "$C$50" and fulfilling some conditions such as dummys (0,1) must increase the age until they is die. "$AT== 1" – Marcelo May 10 '21 at 03:47
  • Thank you @jared_mamrot The line runs perfect but it doesn't increase the value, it just adds up to 1 and 2, I don't know what I'm doing wrong. Conditions 0,1 are already created correctly. I have already passed many columns to R to define the rows and columns. For example, the age of one of the people who begins in the sample is 20, throughout 29 years in the study "$C$50" and fulfilling some conditions such as dummys (0,1) the age must increase until death . "$AT== 1" – Marcelo May 10 '21 at 03:52
  • 2
    It would be easier to help if you create a small reproducible example along with expected output. Read about [how to give a reproducible example](http://stackoverflow.com/questions/5963269). – Ronak Shah May 10 '21 at 05:28

0 Answers0