Re: [R] Looping through data error

2021-04-13 Thread Bert Gunter
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(test

Re: [R] Looping through data error

2021-04-13 Thread e-mail ma015k3113 via R-help
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

Re: [R] Looping through data error

2021-04-13 Thread e-mail ma015k3113 via R-help
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 " }"

Re: [R] Looping through data error

2021-04-13 Thread Rui Barradas
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

Re: [R] Looping through data error

2021-04-13 Thread Rui Barradas
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

Re: [R] Looping through data error

2021-04-13 Thread jim holtman
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:(

[R] Looping through data error

2021-04-13 Thread e-mail ma015k3113 via R-help
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/200