0

I am starting to learn R and I got an error for the loop.

Here is the error I got:

Error in names(data1)[5] <- "TR" : 
'names' attribute [5] must be the same length as the vector [4]

Here is my code:

#read data
data1<-read.table("C_0.txt",header = T,sep=",")

#name the 5th col TR
names(data1)[5]<-"TR"

#calculate the length of data1
n<-nrow(data1)

#initialize first TR value to NA
data1[1,5]<-NA

for (i in 1:(n-1)){

if (data1[i,3]==data1[i+1,3]) {data1[i+1,5]<-data1[i,5]}

if (data1[i,3]< data1[i+1,3]) {data1[i+1,5]<- 1}

if(data1[i,3]> data1[i+1,3]) {data1[i+1,5]<- -1}
}

Here is the algorithm I am trying to codify: if the current price is above the previous price, mark +1 in the column named TR if the current price is below the previous price, mark -1 in the column named TR if the current price is the same as the previous price, mark the same thing as in the previous price in the column named TR

I marked the first TR row as NA because there is no price to compare it to.

here is the data from C_0.txt:

Date,Time,Price,Size
02/18/2014,05:06:13,49.6,200
02/18/2014,05:06:13,49.6,200
02/18/2014,05:06:13,49.6,200
02/18/2014,05:06:14,49.6,200
02/18/2014,05:06:14,49.6,193
02/18/2014,05:44:41,49.62,100
02/18/2014,06:26:36,49.52,100
02/18/2014,06:26:36,49.52,500
02/18/2014,07:09:29,49.6,100
02/18/2014,07:56:40,49.56,300
02/18/2014,07:56:40,49.55,400
02/18/2014,07:56:41,49.54,200
02/18/2014,07:56:43,49.55,100
02/18/2014,07:56:43,49.55,100
02/18/2014,07:56:50,49.55,100
02/18/2014,07:57:12,49.53,100
02/18/2014,07:57:12,49.51,2200
02/18/2014,07:57:12,49.51,100
02/18/2014,07:57:12,49.5,200

Thanks a lot!

user1561949
  • 211
  • 7
  • 16

1 Answers1

2

Adding a column to a data frame is fairly basic stuff. Here's a question that summarises that.

You can't add a column to a data frame by changing the length of the names attribute vector. You have to create it by methods like in this question.

As for the other part of your question, let's use diff and rle instead of a for-loop.

d <-  read.table(sep = ",", text = 
"Date,Time,Price,Size
02/18/2014,05:06:13,49.6,200
02/18/2014,05:06:13,49.6,200
02/18/2014,05:06:13,49.6,200
02/18/2014,05:06:14,49.6,200
02/18/2014,05:06:14,49.6,193
02/18/2014,05:44:41,49.62,100
02/18/2014,06:26:36,49.52,100
02/18/2014,06:26:36,49.52,500
02/18/2014,07:09:29,49.6,100
02/18/2014,07:56:40,49.56,300
02/18/2014,07:56:40,49.55,400
02/18/2014,07:56:41,49.54,200
02/18/2014,07:56:43,49.55,100
02/18/2014,07:56:43,49.55,100
02/18/2014,07:56:50,49.55,100
02/18/2014,07:57:12,49.53,100
02/18/2014,07:57:12,49.51,2200
02/18/2014,07:57:12,49.51,100
02/18/2014,07:57:12,49.5,200", header = TRUE)

tmp <- c(NA, diff(d$Price))
tmp <- sign(tmp)
tmp <- rle(tmp)
l <- tmp$lengths
v <- tmp$values
idx <- which(v == 0L)
l[idx - 1] <- l[idx - 1] + l[idx]
l <- l[-idx]
v <- v[-idx]
d$TR <- inverse.rle(list(lengths = l, values = v))

#          Date     Time Price Size TR
# 1  02/18/2014 05:06:13 49.60  200 NA
# 2  02/18/2014 05:06:13 49.60  200 NA
# 3  02/18/2014 05:06:13 49.60  200 NA
# 4  02/18/2014 05:06:14 49.60  200 NA
# 5  02/18/2014 05:06:14 49.60  193 NA
# 6  02/18/2014 05:44:41 49.62  100  1
# 7  02/18/2014 06:26:36 49.52  100 -1
# 8  02/18/2014 06:26:36 49.52  500 -1
# 9  02/18/2014 07:09:29 49.60  100  1
# 10 02/18/2014 07:56:40 49.56  300 -1
# 11 02/18/2014 07:56:40 49.55  400 -1
# 12 02/18/2014 07:56:41 49.54  200 -1
# 13 02/18/2014 07:56:43 49.55  100  1
# 14 02/18/2014 07:56:43 49.55  100  1
# 15 02/18/2014 07:56:50 49.55  100  1
# 16 02/18/2014 07:57:12 49.53  100 -1
# 17 02/18/2014 07:57:12 49.51 2200 -1
# 18 02/18/2014 07:57:12 49.51  100 -1
# 19 02/18/2014 07:57:12 49.50  200 -1
Community
  • 1
  • 1
Blue Magister
  • 13,044
  • 5
  • 38
  • 56
  • Hi Blue Magister, what does `l[idx - 1] <- l[idx - 1] + l[idx]` do? and what does it mean to negate an index like `l <- l[-idx]`? – user1561949 Feb 26 '14 at 19:57
  • `idx` finds where there are zeroes in the run-length encoding. The first line you question adds the run of zeroes to the previous run. Then `l[-idx]` and `v[-idx]` delete the zeroes. It will help to step through the code, seeing how `l` and `v` change. – Blue Magister Feb 26 '14 at 20:50
  • Thank you Blue Magister for your help – user1561949 Feb 26 '14 at 21:42