Mr. Blueyonder,

There are a number of problems with this.

1) is the data below really in a data frame? I coerced your table into a data frame, guessing at the structure and when I print out the 1st three rows I get an alignment into proper columns

COMPANY_NUMBER COMPANY_NAME                CITY YEAR_END_DATE CLOSE_SHARE_PRICE 1           22705  CARDIFF PROPERTY PUBLIC LIMITED COMPANY          (THE)Egham    30/09/2005                NA 2           22705  CARDIFF PROPERTY PUBLIC LIMITED COMPANY          (THE)Egham    30/09/2006                NA 3           22705  CARDIFF PROPERTY PUBLIC LIMITED COMPANY          (THE)Egham    30/09/2007              9.65

I'd guess that your PLC is actually a 1 column data frame based on how you present it. Because what you gave is so small, I just added commas where they seemed suitable and used

PLC <- read.csv(file='clipboard',header=T,stringsAsFactors = T)

to create the data frame above. It's likely that your actual problem is much larger so I'd export the data as a CSV file, and read it into R in a similar way using

PLC <- read.csv(file='myfile.csv',header=T,stringsAsFactors = T)

2) Your code is incorrect as is for two reasons. a) for your approach to work, you'll need to create an empty column CH_SH_PRICE before running the loop, because your loop is trying to place data into a non-existent column b) the code PLC$CH_SH_PRICE(i+1) has to use square brackets, otherwise R thinks PCL$CH_SH_PRICE is a function rather than a reference to column element i+1 as in PLC$CH_SH_PRICE[i+1]

Also, the functionisTRUE is actually the following

function (x)
is.logical(x) && length(x) == 1L && !is.na(x) && x

Is this really what you want to do, rather than just checking the company number, as inPLC[i,1] == PLC[i+1,1]?

Assuming (tentatively) I guessed right, here's what I got

COMPANY_NUMBER                            COMPANY_NAME CITY YEAR_END_DATE CLOSE_SHARE_PRICE CH_SH_PRICE 1          22705 CARDIFF PROPERTY PUBLIC LIMITED COMPANY (THE)Egham    30/09/2005                NA          NA 2          22705 CARDIFF PROPERTY PUBLIC LIMITED COMPANY (THE)Egham    30/09/2006                NA          NA 3          22705 CARDIFF PROPERTY PUBLIC LIMITED COMPANY (THE)Egham    30/09/2007              9.65          NA 4          22705 CARDIFF PROPERTY PUBLIC LIMITED COMPANY (THE)Egham    30/09/2008              6.55       -3.10

Good luck.

David Stevens

On 3/31/2021 11:04 AM, e-mail ma015k3113 via R-help wrote:
Dear All, I have a data frame which is structured as follows:


COMPANY_NUMBER  COMPANY_NAME    CITY    YEAR_END_DATE   CLOSE_SHARE_PRICE
22705   CARDIFF PROPERTY PUBLIC LIMITED COMPANY         (THE)Egham      
30/09/2005      NA
22705   CARDIFF PROPERTY PUBLIC LIMITED COMPANY         (THE)Egham      
30/09/2006      NA
22705   CARDIFF PROPERTY PUBLIC LIMITED COMPANY         (THE)Egham      
30/09/2007      9.65
22705   CARDIFF PROPERTY PUBLIC LIMITED COMPANY         (THE)Egham      
30/09/2008      6.55
22705   CARDIFF PROPERTY PUBLIC LIMITED COMPANY         (THE)Egham      
30/09/2009      6.55
22705   CARDIFF PROPERTY PUBLIC LIMITED COMPANY         (THE)Egham      
30/09/2010      7.5
10395804        TOC PROPERTY BACKED LENDING TRUST PLC   Newcastle Upon Tyne     
30/11/2016      NA
10395804        TOC PROPERTY BACKED LENDING TRUST PLC   Newcastle Upon Tyne     
30/11/2017      1.04
10395804        TOC PROPERTY BACKED LENDING TRUST PLC   Newcastle Upon Tyne     
30/11/2018      1.04
SC192761        MARTIN CURRIE GLOBAL PORTFOLIO TRUST PLC        Edinburgh       
31/01/2000      NA
SC192761        MARTIN CURRIE GLOBAL PORTFOLIO TRUST PLC        Edinburgh       
31/01/2001      NA
SC192761        MARTIN CURRIE GLOBAL PORTFOLIO TRUST PLC        Edinburgh       
31/01/2003      NA
SC192761        MARTIN CURRIE GLOBAL PORTFOLIO TRUST PLC        Edinburgh       
31/01/2004      NA
SC192761        MARTIN CURRIE GLOBAL PORTFOLIO TRUST PLC        Edinburgh       
31/01/2005      NA
SC192761        MARTIN CURRIE GLOBAL PORTFOLIO TRUST PLC        Edinburgh       
31/01/2006      1.09
SC192761        MARTIN CURRIE GLOBAL PORTFOLIO TRUST PLC        Edinburgh       
31/01/2007      1.17
SC192761        MARTIN CURRIE GLOBAL PORTFOLIO TRUST PLC        Edinburgh       
31/01/2008      1.24
SC192761        MARTIN CURRIE GLOBAL PORTFOLIO TRUST PLC        Edinburgh       
31/01/2009      0.9
SC192761        MARTIN CURRIE GLOBAL PORTFOLIO TRUST PLC        Edinburgh       
31/01/2010      1.14



I am trying to calculate the total change in share price like for CARDIFF 
PROPERTY PUBLIC LIMITED COMPANY the total change

between 2005 and 2006 is NA and for 2006 and 2007 it is NA for 2007 and 2008 it 
is -3.1 and 2008 and 2009 it is 0 and 2009 and 2010 it is +.95.


I am trying to achieve this via he following code:

for (i in 1:(nrow(PLC)-1))
if (isTRUE (PLC[i, 1] == PLC[i + 1, 1]))
{
PLC$CH_SH_PRICE(i+1) = (PLC$CLOSE_SHARE_PRICE[i+1] -
PLC$CLOSE_SHARE_PRICE[i])
}


I get the following error


Error in 1:(nrow(PLC) - 1) : argument of length 0


Can you kindly suggest any solution to this issue?


Thanks in advance.
         [[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.
CAUTION: This email originated from outside of USU. If this appears to be a USU 
employee, beware of impersonators. Do not click links, reply, download images, 
or open attachments unless you verify the sender’s identity and know the 
content is safe.

--
David K Stevens, P.E., Ph.D.
Professor, Environmental Engineering
Civil and Environmental Engineering
Utah Water Research Laboratory
8200 Old Main Hill
Logan, UT  84322-8200
435 797 3229 - voice
435 797 1363 - fax
david.stev...@usu.edu

______________________________________________
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.

Reply via email to