Q:

Compare elements and make new column

I want to compare the i and i+1 elements of second column(Close price), and if i<i+1 place 1 in third column. But i have no idea how to do this.
require(quantmod)
getSymbols("GOOG",src="yahoo")
data<-last(GOOG,40)
newdata<-data.frame(data[,4])
newdata

A:

Try this:
newdata$closePrice = c((diff(newdata[,1])<0)*1,0)

Explanation:
diff(newdata[,1]) will return the difference of rows i+1 and i
The <0 will give a true or false array that can be converted to 1s or 0s
Since the result will be one element short, concatenate a 0 at the end

