Skip to content
Prev 387676 / 398502 Next

[EXT] Calculating the total change in shareprice over a time periond

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: