Re: [R] Looping through data error
Well, if I understand your query, wouldn't the following simple approach suffice -- it assumes that the results for each company are ordered by year, as your example seems to show: ## test is your example data ## first remove NA's test2 <- na.omit(test) ## Now just use tapply(): > out <-with(test2, tapply(CLOSE_SHARE_PRICE, COMPANY_NUMBER, + FUN =function(x)100 /x[1])) > out 1091347 1135606922705 SC192761 12.28501 91.74312 15.26718 91.74312 ## essentially a labelled vector ##You can use %/% if you only want the whole number of shares that can be purchased It's somewhat messier if the results are not ordered by date within company -- you could use by() and POSIXct to order the dates within company to get the right one. Cheers, Bert Gunter "The trouble with having an open mind is that people keep coming along and sticking things into it." -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) On Tue, Apr 13, 2021 at 11:34 AM e-mail ma015k3113 via R-help < r-help@r-project.org> wrote: > Rui, excellent diagnosis and suggestion. It worked but my damn logic is > still not delivering what I want-will spend more time on it tomorrow. > > > Kind regards > > Ahson > > > On 13 April 2021 at 17:06 Rui Barradas wrote: > > > > > > Hello, > > > > A close parenthesis is missing in the nd if. > > > > > > for (i in 1:(nrow(PLC_Return)-1)){ > >if (i == 1){ > > NUMBER_OF_SHARES[i] = 100/is.na(CLOSE_SHARE_PRICE[i]) > >} else if(is.na(PLC_Return[i, 1]) == is.na(PLC_Return[i + 1, 1])){ > > NUMBER_OF_SHARES[i]=0 > >} else { > > NUMBER_OF_SHARES[i] = 100/is.na(CLOSE_SHARE_PRICE[i]) > >} > > } > > > > > > Hope this helps, > > > > Rui Barradas > > > > Às 13:51 de 13/04/21, e-mail ma015k3113 via R-help escreveu: > > > Dear All,I have a dataframe with 4 variables and I am trying to > calculate how many shares can be purchased with £100 in the first year when > the company was listed > > > > > > The data looks like: > > > > > > COMPANY_NUMBER YEAR_END_DATE CLOSE_SHARE_PRICE NUMBER_OF_SHARES > > > 2270530/09/2002 > NA 0 > > > 2270530/09/2004 > NA 0 > > > 2270530/09/2005 > 6.55 0 > > > 2270530/09/2006 > 7.5 0 > > > 2270530/09/2007 > 9.65 0 > > > 2270530/09/2008 > 6.55 0 > > > 109134731/01/2010 > 8.14 0 > > > 1091347 31/01/2011 > 11.38 0 > > > 11356069 30/06/2019 > 1.09 0 > > > SC192761 31/01/2000 > NA 0 > > > SC192761 31/01/2001 > NA 0 > > > SC192761 31/01/2002 > NA 0 > > > SC192761 31/01/2004 > NA 0 > > > SC192761 31/01/2005 > NA 0 > > > SC192761 31/01/2006 > 1.09 0 > > > SC192761 31/01/2008 > 1.24 0 > > > SC192761 31/01/2009 > 0.90 > > > SC192761 31/01/2010 1.14 > 0 > > > SC192761 31/01/20111.25 > 0 > > > SC192761 31/01/2012 1.29 > 0 > > > > > > > > > The code I have written is > > > > > > i <- 0 > > > > > > for (i in 1:(nrow(PLC_Return)-1)) > > > if (i == 1) > > > { > > > NUMBER_OF_SHARES[i] = 100/is.na(CLOSE_SHARE_PRICE[i]) > > > } else if > > > (is.na(PLC_Return[i, 1]) == is.na(PLC_Return[i + 1, 1]) > > > { > > > NUMBER_OF_SHARES[i]=0 > > > } else > > > { > > > NUMBER_OF_SHARES[i] = 100/is.na(CLOSE_SHARE_PRICE[i]) > > > } > > > > > > > > > The error I get is Error: unexpected 'else' in: > > > > > > " NUMBER_OF_SHARES[i] = 0 > > > } else" > > >> {NUMBER_OF_SHARES[i] = 100/is.na(CLOSE_SHARE_PRICE[i])} > > >> > > >> } > > > Error: unexpected '}' in "}" > > > > > > > > > Don't know how to fix it-any help will be appreciated. > > > > > > > > > Kind regards > > > > > > > > > Ahson > > > [[alternative HTML version deleted]] > > > > > > __ > > > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > > >
Re: [R] Looping through data error
Rui, excellent diagnosis and suggestion. It worked but my damn logic is still not delivering what I want-will spend more time on it tomorrow. Kind regards Ahson > On 13 April 2021 at 17:06 Rui Barradas wrote: > > > Hello, > > A close parenthesis is missing in the nd if. > > > for (i in 1:(nrow(PLC_Return)-1)){ >if (i == 1){ > NUMBER_OF_SHARES[i] = 100/is.na(CLOSE_SHARE_PRICE[i]) >} else if(is.na(PLC_Return[i, 1]) == is.na(PLC_Return[i + 1, 1])){ > NUMBER_OF_SHARES[i]=0 >} else { > NUMBER_OF_SHARES[i] = 100/is.na(CLOSE_SHARE_PRICE[i]) >} > } > > > Hope this helps, > > Rui Barradas > > Às 13:51 de 13/04/21, e-mail ma015k3113 via R-help escreveu: > > Dear All,I have a dataframe with 4 variables and I am trying to calculate > > how many shares can be purchased with £100 in the first year when the > > company was listed > > > > The data looks like: > > > > COMPANY_NUMBER YEAR_END_DATE CLOSE_SHARE_PRICE NUMBER_OF_SHARES > > 2270530/09/2002 NA > > 0 > > 2270530/09/2004 NA > > 0 > > 2270530/09/20056.55 > > 0 > > 2270530/09/20067.5 > > 0 > > 2270530/09/20079.65 > > 0 > > 2270530/09/20086.55 > > 0 > > 109134731/01/20108.14 > > 0 > > 1091347 31/01/2011 11.38 > > 0 > > 11356069 30/06/2019 1.09 > > 0 > > SC192761 31/01/2000 NA > > 0 > > SC192761 31/01/2001 NA > > 0 > > SC192761 31/01/2002NA > > 0 > > SC192761 31/01/2004 NA > > 0 > > SC192761 31/01/2005 NA > > 0 > > SC192761 31/01/2006 1.09 > > 0 > > SC192761 31/01/2008 1.24 > > 0 > > SC192761 31/01/2009 0.9 > > 0 > > SC192761 31/01/2010 1.14 > > 0 > > SC192761 31/01/20111.25 > > 0 > > SC192761 31/01/2012 1.29 > > 0 > > > > > > The code I have written is > > > > i <- 0 > > > > for (i in 1:(nrow(PLC_Return)-1)) > > if (i == 1) > > { > > NUMBER_OF_SHARES[i] = 100/is.na(CLOSE_SHARE_PRICE[i]) > > } else if > > (is.na(PLC_Return[i, 1]) == is.na(PLC_Return[i + 1, 1]) > > { > > NUMBER_OF_SHARES[i]=0 > > } else > > { > > NUMBER_OF_SHARES[i] = 100/is.na(CLOSE_SHARE_PRICE[i]) > > } > > > > > > The error I get is Error: unexpected 'else' in: > > > > " NUMBER_OF_SHARES[i] = 0 > > } else" > >> {NUMBER_OF_SHARES[i] = 100/is.na(CLOSE_SHARE_PRICE[i])} > >> > >> } > > Error: unexpected '}' in "}" > > > > > > Don't know how to fix it-any help will be appreciated. > > > > > > Kind regards > > > > > > Ahson > > [[alternative HTML version deleted]] > > > > __ > > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > > https://stat.ethz.ch/mailman/listinfo/r-help > > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > > and provide commented, minimal, self-contained, reproducible code. > > __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Looping through data error
Jim, thanks for taking the time to look into this. Yes, these if else statements are so confusing. I tried your amended scode and it does not work. The error are as follows: Error: unexpected '}' in " }" > NUMBER_OF_SHARES[i] = 100 / is.na(CLOSE_SHARE_PRICE[i]) > } Error: unexpected '}' in " }" > } Error: unexpected '}' in " }" > } Error: unexpected '}' in "}" > I have spent so much time on this-hopefully I will come to grips sooner or later. In the mean time any further suggestion? Kind regards Ahson > On 13 April 2021 at 14:29 jim holtman wrote: > > Your code was formatted incorrectly. There is always a problem with the > 'else' statement after an 'if' since in R there is no semicolon to mark the > end of a line. Here might be a better format for your code. I would > recommend the liberal use of "{}"s when using 'if/else' > > > > i <- 0 > > for (i in 1:(nrow(PLC_Return) - 1)) { > if (i == 1) { > NUMBER_OF_SHARES[i] = 100 /http://is.na (CLOSE_SHARE_PRICE[i]) > } else { > if (http://is.na (PLC_Return[i, 1]) ==http://is.na (PLC_Return[i + 1, > 1]) { > NUMBER_OF_SHARES[i] = 0 > } else { > NUMBER_OF_SHARES[i] = 100 /http://is.na (CLOSE_SHARE_PRICE[i]) > } > } > } > > > Jim Holtman > Data Munger Guru > > What is the problem that you are trying to solve? > Tell me what you want to do, not how you want to do it. > > > On Tue, Apr 13, 2021 at 5:51 AM e-mail ma015k3113 via R-help < > r-help@r-project.org mailto:r-help@r-project.org > wrote: > > > > Dear All,I have a dataframe with 4 variables and I am trying to > calculate how many shares can be purchased with £100 in the first year when > the company was listed > > > > The data looks like: > > > > COMPANY_NUMBER YEAR_END_DATE CLOSE_SHARE_PRICE NUMBER_OF_SHARES > > 2270530/09/2002 > > NA 0 > > 2270530/09/2004 > > NA 0 > > 2270530/09/2005 > >6.55 0 > > 2270530/09/2006 > >7.5 0 > > 2270530/09/2007 > >9.65 0 > > 2270530/09/2008 > >6.55 0 > > 109134731/01/2010 > > 8.14 0 > > 1091347 31/01/2011 > > 11.38 0 > > 11356069 30/06/2019 > > 1.09 0 > > SC192761 31/01/2000 > > NA 0 > > SC192761 31/01/2001 > > NA 0 > > SC192761 31/01/2002 > > NA 0 > > SC192761 31/01/2004 > > NA 0 > > SC192761 31/01/2005 > > NA 0 > > SC192761 31/01/2006 > > 1.09 0 > > SC192761 31/01/2008 > > 1.24 0 > > SC192761 31/01/2009 > > 0.90 > > SC192761 31/01/2010 > > 1.140 > > SC192761 31/01/2011 > > 1.250 > > SC192761 31/01/2012 > > 1.290 > > > > > > The code I have written is > > > > i <- 0 > > > > for (i in 1:(nrow(PLC_Return)-1)) > > if (i == 1) > > { > > NUMBER_OF_SHARES[i] = 100/http://is.na (CLOSE_SHARE_PRICE[i]) > > } else if > > (http://is.na (PLC_Return[i, 1]) ==http://is.na (PLC_Return[i + 1, > > 1]) > > { > > NUMBER_OF_SHARES[i]=0 > > } else > > { > > NUMBER_OF_SHARES[i] = 100/http://is.na (CLOSE_SHARE_PRICE[i]) > > } > > > > > > The error I get is Error: unexpected 'else' in: > >
Re: [R] Looping through data error
Hello, Typo, inline. Às 17:06 de 13/04/21, Rui Barradas escreveu: Hello, A close parenthesis is missing in the nd if. Should be "the 2nd if". Rui Barradas for (i in 1:(nrow(PLC_Return)-1)){ if (i == 1){ NUMBER_OF_SHARES[i] = 100/is.na(CLOSE_SHARE_PRICE[i]) } else if(is.na(PLC_Return[i, 1]) == is.na(PLC_Return[i + 1, 1])){ NUMBER_OF_SHARES[i]=0 } else { NUMBER_OF_SHARES[i] = 100/is.na(CLOSE_SHARE_PRICE[i]) } } Hope this helps, Rui Barradas Às 13:51 de 13/04/21, e-mail ma015k3113 via R-help escreveu: Dear All,I have a dataframe with 4 variables and I am trying to calculate how many shares can be purchased with £100 in the first year when the company was listed The data looks like: COMPANY_NUMBER YEAR_END_DATE CLOSE_SHARE_PRICE NUMBER_OF_SHARES 22705 30/09/2002 NA 0 22705 30/09/2004 NA 0 22705 30/09/2005 6.55 0 22705 30/09/2006 7.5 0 22705 30/09/2007 9.65 0 22705 30/09/2008 6.55 0 1091347 31/01/2010 8.14 0 1091347 31/01/2011 11.38 0 11356069 30/06/2019 1.09 0 SC192761 31/01/2000 NA 0 SC192761 31/01/2001 NA 0 SC192761 31/01/2002 NA 0 SC192761 31/01/2004 NA 0 SC192761 31/01/2005 NA 0 SC192761 31/01/2006 1.09 0 SC192761 31/01/2008 1.24 0 SC192761 31/01/2009 0.9 0 SC192761 31/01/2010 1.14 0 SC192761 31/01/2011 1.25 0 SC192761 31/01/2012 1.29 0 The code I have written is i <- 0 for (i in 1:(nrow(PLC_Return)-1)) if (i == 1) { NUMBER_OF_SHARES[i] = 100/is.na(CLOSE_SHARE_PRICE[i]) } else if (is.na(PLC_Return[i, 1]) == is.na(PLC_Return[i + 1, 1]) { NUMBER_OF_SHARES[i]=0 } else { NUMBER_OF_SHARES[i] = 100/is.na(CLOSE_SHARE_PRICE[i]) } The error I get is Error: unexpected 'else' in: " NUMBER_OF_SHARES[i] = 0 } else" {NUMBER_OF_SHARES[i] = 100/is.na(CLOSE_SHARE_PRICE[i])} } Error: unexpected '}' in "}" Don't know how to fix it-any help will be appreciated. Kind regards Ahson [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Looping through data error
Hello, A close parenthesis is missing in the nd if. for (i in 1:(nrow(PLC_Return)-1)){ if (i == 1){ NUMBER_OF_SHARES[i] = 100/is.na(CLOSE_SHARE_PRICE[i]) } else if(is.na(PLC_Return[i, 1]) == is.na(PLC_Return[i + 1, 1])){ NUMBER_OF_SHARES[i]=0 } else { NUMBER_OF_SHARES[i] = 100/is.na(CLOSE_SHARE_PRICE[i]) } } Hope this helps, Rui Barradas Às 13:51 de 13/04/21, e-mail ma015k3113 via R-help escreveu: Dear All,I have a dataframe with 4 variables and I am trying to calculate how many shares can be purchased with £100 in the first year when the company was listed The data looks like: COMPANY_NUMBER YEAR_END_DATE CLOSE_SHARE_PRICE NUMBER_OF_SHARES 2270530/09/2002 NA 0 2270530/09/2004 NA 0 2270530/09/20056.55 0 2270530/09/20067.5 0 2270530/09/20079.65 0 2270530/09/20086.55 0 109134731/01/20108.14 0 1091347 31/01/2011 11.38 0 11356069 30/06/2019 1.09 0 SC192761 31/01/2000 NA 0 SC192761 31/01/2001 NA 0 SC192761 31/01/2002NA 0 SC192761 31/01/2004 NA 0 SC192761 31/01/2005 NA 0 SC192761 31/01/2006 1.09 0 SC192761 31/01/2008 1.24 0 SC192761 31/01/2009 0.9 0 SC192761 31/01/2010 1.14 0 SC192761 31/01/20111.25 0 SC192761 31/01/2012 1.29 0 The code I have written is i <- 0 for (i in 1:(nrow(PLC_Return)-1)) if (i == 1) { NUMBER_OF_SHARES[i] = 100/is.na(CLOSE_SHARE_PRICE[i]) } else if (is.na(PLC_Return[i, 1]) == is.na(PLC_Return[i + 1, 1]) { NUMBER_OF_SHARES[i]=0 } else { NUMBER_OF_SHARES[i] = 100/is.na(CLOSE_SHARE_PRICE[i]) } The error I get is Error: unexpected 'else' in: " NUMBER_OF_SHARES[i] = 0 } else" {NUMBER_OF_SHARES[i] = 100/is.na(CLOSE_SHARE_PRICE[i])} } Error: unexpected '}' in "}" Don't know how to fix it-any help will be appreciated. Kind regards Ahson [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Looping through data error
Your code was formatted incorrectly. There is always a problem with the 'else' statement after an 'if' since in R there is no semicolon to mark the end of a line. Here might be a better format for your code. I would recommend the liberal use of "{}"s when using 'if/else' i <- 0 for (i in 1:(nrow(PLC_Return) - 1)) { if (i == 1) { NUMBER_OF_SHARES[i] = 100 / is.na(CLOSE_SHARE_PRICE[i]) } else { if (is.na(PLC_Return[i, 1]) == is.na(PLC_Return[i + 1, 1]) { NUMBER_OF_SHARES[i] = 0 } else { NUMBER_OF_SHARES[i] = 100 / is.na(CLOSE_SHARE_PRICE[i]) } } } Jim Holtman *Data Munger Guru* *What is the problem that you are trying to solve?Tell me what you want to do, not how you want to do it.* On Tue, Apr 13, 2021 at 5:51 AM e-mail ma015k3113 via R-help < r-help@r-project.org> wrote: > Dear All,I have a dataframe with 4 variables and I am trying to calculate > how many shares can be purchased with £100 in the first year when the > company was listed > > The data looks like: > > COMPANY_NUMBER YEAR_END_DATE CLOSE_SHARE_PRICE NUMBER_OF_SHARES > 2270530/09/2002 > NA 0 > 2270530/09/2004 > NA 0 > 2270530/09/2005 > 6.55 0 > 2270530/09/2006 > 7.5 0 > 2270530/09/2007 > 9.65 0 > 2270530/09/2008 > 6.55 0 > 109134731/01/20108.14 >0 > 1091347 31/01/2011 11.38 >0 > 11356069 30/06/2019 1.09 >0 > SC192761 31/01/2000 NA >0 > SC192761 31/01/2001 NA >0 > SC192761 31/01/2002NA >0 > SC192761 31/01/2004 NA >0 > SC192761 31/01/2005 NA >0 > SC192761 31/01/2006 1.09 >0 > SC192761 31/01/2008 1.24 >0 > SC192761 31/01/2009 0.9 > 0 > SC192761 31/01/2010 1.14 > 0 > SC192761 31/01/20111.25 > 0 > SC192761 31/01/2012 1.29 > 0 > > > The code I have written is > > i <- 0 > > for (i in 1:(nrow(PLC_Return)-1)) > if (i == 1) > { > NUMBER_OF_SHARES[i] = 100/is.na(CLOSE_SHARE_PRICE[i]) > } else if > (is.na(PLC_Return[i, 1]) == is.na(PLC_Return[i + 1, 1]) > { > NUMBER_OF_SHARES[i]=0 > } else > { > NUMBER_OF_SHARES[i] = 100/is.na(CLOSE_SHARE_PRICE[i]) > } > > > The error I get is Error: unexpected 'else' in: > > " NUMBER_OF_SHARES[i] = 0 > } else" > > {NUMBER_OF_SHARES[i] = 100/is.na(CLOSE_SHARE_PRICE[i])} > > > > } > Error: unexpected '}' in "}" > > > Don't know how to fix it-any help will be appreciated. > > > Kind regards > > > Ahson > [[alternative HTML version deleted]] > > __ > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] Looping through data error
Dear All,I have a dataframe with 4 variables and I am trying to calculate how many shares can be purchased with £100 in the first year when the company was listed The data looks like: COMPANY_NUMBER YEAR_END_DATE CLOSE_SHARE_PRICE NUMBER_OF_SHARES 2270530/09/2002 NA 0 2270530/09/2004 NA 0 2270530/09/20056.55 0 2270530/09/20067.5 0 2270530/09/20079.65 0 2270530/09/20086.55 0 109134731/01/20108.14 0 1091347 31/01/2011 11.38 0 11356069 30/06/2019 1.09 0 SC192761 31/01/2000 NA 0 SC192761 31/01/2001 NA 0 SC192761 31/01/2002NA 0 SC192761 31/01/2004 NA 0 SC192761 31/01/2005 NA 0 SC192761 31/01/2006 1.09 0 SC192761 31/01/2008 1.24 0 SC192761 31/01/2009 0.9 0 SC192761 31/01/2010 1.14 0 SC192761 31/01/20111.25 0 SC192761 31/01/2012 1.29 0 The code I have written is i <- 0 for (i in 1:(nrow(PLC_Return)-1)) if (i == 1) { NUMBER_OF_SHARES[i] = 100/is.na(CLOSE_SHARE_PRICE[i]) } else if (is.na(PLC_Return[i, 1]) == is.na(PLC_Return[i + 1, 1]) { NUMBER_OF_SHARES[i]=0 } else { NUMBER_OF_SHARES[i] = 100/is.na(CLOSE_SHARE_PRICE[i]) } The error I get is Error: unexpected 'else' in: " NUMBER_OF_SHARES[i] = 0 } else" > {NUMBER_OF_SHARES[i] = 100/is.na(CLOSE_SHARE_PRICE[i])} > > } Error: unexpected '}' in "}" Don't know how to fix it-any help will be appreciated. Kind regards Ahson [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.