Question



Sorting by stock symbol

Dear Dr. Balling,

I ran the following code:

# sort by Symbol, Date
attach(DATA)
DATA1 <- DATA[order(Symbol, Date),]


Next, I ran code to find the first 6 rows and last 6 rows after sorting by Symbol and Date.
It is shown below with the output.

head(DATA1)
# Symbol Date Open High Low Close Volume DV
# 1 A 2001-01-01 54.75 54.75 54.75 54.75 0 1
# 1307 A 2001-01-02 53.88 53.88 49.06 50.88 1617800 0
# 2613 A 2001-01-03 49.13 56.50 47.56 56.13 3220800 1
# 3919 A 2001-01-04 56.88 59.69 55.31 58.25 3146200 1
# 5226 A 2001-01-05 57.25 58.25 53.50 55.06 2344100 0
# 6533 A 2001-01-08 54.25 55.75 52.31 53.25 1626100 0

DATA1[348166:348171,]
# Symbol Date Open High Low Close Volume DV
# 347776 PCN 2001-12-31 15.02 15.08 15.00 15.02 40500 1
# 345047 PFX 2001-12-27 25.50 25.50 24.50 24.85 1400 0
# 346419 PFX 2001-12-28 25.25 25.50 24.85 24.85 4100 0
# 347791 PFX 2001-12-31 25.11 25.11 25.00 25.00 2500 0
# 345917 FSB 2001-12-28 25.02 25.20 25.00 25.15 40400 1
# 347289 FSB 2001-12-31 25.04 25.04 25.03 25.03 1600 0


Not only do the last rows not show the last alphabetical symbols, the last rows are mixed together with PCN, PFX, and FSB.

I would think that the last six rows would show something like

# Symbol Date Open High Low Close Volume DV
# 347776 ZZ 2001-12-26 15.02 15.08 15.00 15.02 40500 1
# 345047 ZZ 2001-12-27 25.50 25.50 24.50 24.85 1400 0
# 346419 ZZ 2001-12-28 25.25 25.50 24.85 24.85 4100 0
# 347791 ZZ 2001-12-29 25.11 25.11 25.00 25.00 2500 0
# 345917 ZZ 2001-12-30 25.02 25.20 25.00 25.15 40400 1
# 347289 ZZ 2001-12-31 25.04 25.04 25.03 25.03 1600 0


How can I make it to sort in the way that I want? I have tried several codes from Google and none of them give me the output that I am
looking for.

Thanks for your help.





Answers and follow-up questions





Answer or follow-up question 1

I apologize for misspelling your name as Dr. Balling instead of Dr. Ballings.


Answer or follow-up question 2

I figured it out. I was reading in the data wrong to begin with.





Sign in to be able to add an answer or mark this question as resolved.