0
votes

I have a dataframe with 3 variables and I am trying to calculate how many chares can be purchased with £100 in the first year when the company was listed

The data looks like

YEAR_END COMPANY_NUMBER CLOSE_SHARE_PRICE NUMBER_OF_SHARES 31/12/2002 22705 7.5 0
31/12/2003 22705 6.55 0 31/12/2004 22705 6.55 0 31/12/2005 22705 NA 0 31/12/2006 22705 NA 0 31/12/2007 22705 NA 0 31/12/2008 22705 NA 0 31/12/2004 11356069 1.09 0 31/12/2002 SC192761 2.42 0 31/12/2003 SC192761 0.9 0 31/12/2004 SC192761 NA 0 31/12/2005 SC192761 NA 0 31/12/2006 SC192761 NA 0 31/12/2007 SC192761 NA 0 31/12/2002 10395804 1.04 0 31/12/2003 10395804 1.04 0 31/12/2004 10395804 NA 0 31/12/2003 5625107 0.02 0 31/12/2004 5625107 0 0

The code I have written is:

 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])
   }

basically it is that for the first company year calculate the shares purchased. Then in the send year I don't need the calculation.

 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])
   }

I get an error Error: unexpected '}' in " }"

   {
  •    NUMBER_OF_SHARES[i] = 100/is.na(CLOSE_SHARE_PRICE[i])
    

Can anyone help me resolve this?

Thanks in advance.

Kind regards

Ahson

1

1 Answers

0
votes

May be a proper indentation and for loop block could help

for (i in 1:(nrow(PLC_Return)-1))
{
      if (i == 1)
         {
              NUMBER_OF_SHARES[i] = 
              1000/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] = 
             1100is.na(CLOSE_SHARE_PRICE[i])
   }
}