Read a price file into a data frame with read.csv, check its shape and types, turn the date column into real dates, and pull one ticker out of it.
read.csv("prices.csv") reads a file of comma separated text and hands you back a data frame: one column per field in the header, one row per line in the file.
In this lesson I read a price file, check what came in, fix the date column, and pull a single ticker out of it. The file is simulated, not real market data.
Step 1. Read the file and check its shape
The file sits next to the lesson, so the name on its own is enough. dim() gives rows then columns.
p <-read.csv("prices.csv") # read the file into a data frameprint(dim(p)) # -> [1] 120 4
chr is character, num is numeric, int is integer. read.csv read the numbers as numbers, so Close and Volume are ready to compute with. Date is text.
summary() on a numeric column gives the six figure summary.
print(summary(p$Close)) # spread of the closes, all tickers mixed
Min. 1st Qu. Median Mean 3rd Qu. Max.
101.4 122.1 186.0 244.4 431.1 450.3
# -> Min. 1st Qu. Median Mean 3rd Qu. Max.# -> 101.4 122.1 186.0 244.4 431.1 450.3
That mixes all three tickers into one column, so the mean of 244.4 is not a price anything traded at. It sits between DDD near 110 and CCC near 430. Split by ticker before you read anything into it.
Step 2. The date column arrives as text
class() says what a column is. Ask it before you trust a column.
print(class(p$Date)) # -> [1] "character"
[1] "character"
print(p$Date[1]) # -> [1] "2026-01-02"
[1] "2026-01-02"
The quotes give it away. It is a string of ten characters, not a date.
Text sorts as text, character by character. Here are three dates held as text in day/month/year form.
messy <-c("1/2/2026", "10/1/2026", "2/3/2026") # three dates held as textprint(sort(messy)) # -> [1] "1/2/2026" "10/1/2026" "2/3/2026"
[1] "1/2/2026" "10/1/2026" "2/3/2026"
October landed in the middle, because "1" sorts before "2". as.Date() turns text into a date, and dates sort and subtract by calendar.
p$Date <-as.Date(p$Date) # convert the column in placeprint(class(p$Date)) # -> [1] "Date"
[1] "Date"
print(p$Date[1]) # -> [1] "2026-01-02"
[1] "2026-01-02"
class() now says Date. The printed value looks the same, so the class is the only proof.
The column is stored as a count of days, so subtraction works.
span <-max(p$Date) -min(p$Date) # last date minus first dateprint(span) # -> Time difference of 55 days
Time difference of 55 days
Try that on the character column and R stops with non-numeric argument to binary operator. Convert the date column right after you read the file, every time.
Step 3. Count what is in the file
table() counts how many times each value appears in a column.
print(table(p$Ticker)) # how many rows each ticker has
AAA CCC DDD
40 40 40
# -># -> AAA CCC DDD# -> 40 40 40
Three tickers, 40 rows each. range() gives the smallest and largest value, which on a date column is the span of the file.
40 distinct dates and 40 rows per ticker, so no ticker is missing a day.
Step 4. Pull one ticker out
p$Ticker == "AAA" gives one TRUE or FALSE per row. Put that vector inside [ , ] before the comma and R keeps the TRUE rows and every column. The comma is what makes it a row filter; forget it and R tries to select columns instead.
keep <- p$Ticker =="AAA"# TRUE on the AAA rowsprint(sum(keep)) # -> [1] 40
[1] 40
aapl <- p[keep, ] # rows where keep is TRUE, all columnsprint(dim(aapl)) # -> [1] 40 4
[1] 40 4
sum() on a logical vector counts the TRUEs, because TRUE counts as 1.
print(head(aapl, 5)) # first five AAA rows, row numbers kept
The row numbers went 1, 4, 7, 10, 13. They are the positions those rows held in p, kept as labels. Set rownames(aapl) <- NULL if you want them renumbered from 1.
order(x) returns the positions of x sorted smallest first, and feeding those positions back as a row index sorts the frame. Here the file already arrives in date order, so nothing moves. Reverse the rows first and you can see the sort do its work.
back <- aapl[nrow(aapl):1, ] # same rows, newest firstprint(head(back, 3)) # newest three now at the top
Date Ticker Close Volume
118 2026-02-26 AAA 169.42 6496907
115 2026-02-25 AAA 171.42 5647098
112 2026-02-24 AAA 172.64 7223495
# -> Date Ticker Close Volume# -> 118 2026-02-26 AAA 169.42 6496907# -> 115 2026-02-25 AAA 171.42 5647098# -> 112 2026-02-24 AAA 172.64 7223495fixed <- back[order(back$Date), ] # sort the rows by date, oldest firstprint(head(fixed, 3)) # oldest three back at the top
Date Ticker Close Volume
1 2026-01-02 AAA 186.66 2183494
4 2026-01-05 AAA 186.38 8463199
7 2026-01-06 AAA 188.51 5299573