This is an assignment to use R to manipulate columns and rows of data and to clean it and create a subset of the data.

Data will first be loaded from a file on my github. The mean and median of 2 attibutes will be displayed and then a new dataframe will be created with a subset of the rows and columns.

This corresponds to items 1,2 and 7 in the assignment.
library(data.table)
URL<-"https://raw.githubusercontent.com/WigodskyD/R-Bridge/master/Weekly.csv"
SP500<-fread(URL)
summary(SP500)
##        V1            Year           Lag1               Lag2         
##  Min.   :   1   Min.   :1990   Min.   :-18.1950   Min.   :-18.1950  
##  1st Qu.: 273   1st Qu.:1995   1st Qu.: -1.1540   1st Qu.: -1.1540  
##  Median : 545   Median :2000   Median :  0.2410   Median :  0.2410  
##  Mean   : 545   Mean   :2000   Mean   :  0.1506   Mean   :  0.1511  
##  3rd Qu.: 817   3rd Qu.:2005   3rd Qu.:  1.4050   3rd Qu.:  1.4090  
##  Max.   :1089   Max.   :2010   Max.   : 12.0260   Max.   : 12.0260  
##       Lag3               Lag4               Lag5         
##  Min.   :-18.1950   Min.   :-18.1950   Min.   :-18.1950  
##  1st Qu.: -1.1580   1st Qu.: -1.1580   1st Qu.: -1.1660  
##  Median :  0.2410   Median :  0.2380   Median :  0.2340  
##  Mean   :  0.1472   Mean   :  0.1458   Mean   :  0.1399  
##  3rd Qu.:  1.4090   3rd Qu.:  1.4090   3rd Qu.:  1.4050  
##  Max.   : 12.0260   Max.   : 12.0260   Max.   : 12.0260  
##      Volume            Today           Direction        
##  Min.   :0.08747   Min.   :-18.1950   Length:1089       
##  1st Qu.:0.33202   1st Qu.: -1.1540   Class :character  
##  Median :1.00268   Median :  0.2410   Mode  :character  
##  Mean   :1.57462   Mean   :  0.1499                     
##  3rd Qu.:2.05373   3rd Qu.:  1.4050                     
##  Max.   :9.32821   Max.   : 12.0260
SP500[[11]]<-sign(SP500[[3]] - SP500[[7]])
SP500subset<-SP500[1:500,c(1,2,7,8,9,10,11)]

The mean weekly return is 0.149899.
The median weekly return is 0.241.
The mean weekly trading volume is 1.5746176 billion.
The median weekly trading volume is 1.00268 billion.

Columns will be renamed.

The mean and median have both changed considerably.

The earlier half of the period covered had better returns and a lot less trading volume.

This correspond to items 3 and 4 in the assignment.
colnames(SP500subset)<-c("Week","Year","Returns_Lagged_5_Days", "Average_Daily_Vol._in_Billions","Weekly_Returns","Direction", "Previous_4_Day_Trend_Change")
summary(SP500subset)
##       Week            Year      Returns_Lagged_5_Days
##  Min.   :  1.0   Min.   :1990   Min.   :-5.1840      
##  1st Qu.:125.8   1st Qu.:1992   1st Qu.:-0.9155      
##  Median :250.5   Median :1994   Median : 0.3250      
##  Mean   :250.5   Mean   :1994   Mean   : 0.2782      
##  3rd Qu.:375.2   3rd Qu.:1997   3rd Qu.: 1.3228      
##  Max.   :500.0   Max.   :1999   Max.   : 7.3170      
##  Average_Daily_Vol._in_Billions Weekly_Returns     Direction        
##  Min.   :0.08747                Min.   :-5.1840   Length:500        
##  1st Qu.:0.20696                1st Qu.:-0.9005   Class :character  
##  Median :0.31004                Median : 0.3415   Mode  :character  
##  Mean   :0.37060                Mean   : 0.2965                     
##  3rd Qu.:0.50022                3rd Qu.: 1.3228                     
##  Max.   :0.95852                Max.   : 7.3170                     
##  Previous_4_Day_Trend_Change
##  Min.   :-1.000             
##  1st Qu.:-1.000             
##  Median : 1.000             
##  Mean   : 0.034             
##  3rd Qu.: 1.000             
##  Max.   : 1.000

The subset’s mean weekly return is 0.296548.
The subset’s median weekly return is 0.3415.
The subset’s mean weekly trading volume is 0.3706017 billion.
The subset’s median weekly trading volume is 0.310037 billion.

Rename values within the rows to make them more useful to manipulate or easier to read.

This corresponds to item 5 in the assignment.
SP500subset$Year[SP500subset$Year == "1990"] <- "First Year"
SP500subset$Year[SP500subset$Year == "1991"] <- "Second Year"
SP500subset$Year[SP500subset$Year == "1992"] <- "Third Year"
SP500subset$Year[SP500subset$Year == "1993"] <- "Fourth Year"
SP500subset$Year[SP500subset$Year == "1994"] <- "Fifth Year"
SP500subset$Year[SP500subset$Year == "1995"] <- "Sixth Year"
SP500subset$Year[SP500subset$Year == "1996"] <- "Seventh Year"
SP500subset$Year[SP500subset$Year == "1997"] <- "Eighth Year"
SP500subset$Year[SP500subset$Year == "1998"] <- "Ninth Year"
SP500subset$Year[SP500subset$Year == "1999"] <- "Tenth Year"

SP500subset[SP500subset == "Up"] <-1
SP500subset[SP500subset == "Down"] <--1
SP500subset[[6]]<-as.numeric(SP500subset[[6]])
SP500subset[[8]]<-abs((SP500subset[[6]] - SP500subset[[7]])/2)

Display what the data frame looks like after manipulation.

This corresponds to item 6 in the assignment.
str(SP500subset)
## Classes 'data.table' and 'data.frame':   500 obs. of  8 variables:
##  $ Week                          : int  1 2 3 4 5 6 7 8 9 10 ...
##  $ Year                          : chr  "First Year" "First Year" "First Year" "First Year" ...
##  $ Returns_Lagged_5_Days         : num  -3.484 -0.229 -3.936 1.572 0.816 ...
##  $ Average_Daily_Vol._in_Billions: num  0.155 0.149 0.16 0.162 0.154 ...
##  $ Weekly_Returns                : num  -0.27 -2.576 3.514 0.712 1.178 ...
##  $ Direction                     : num  -1 -1 1 1 1 -1 1 1 1 -1 ...
##  $ Previous_4_Day_Trend_Change   : num  1 -1 1 1 -1 1 1 -1 -1 1 ...
##  $ V8                            : num  1 0 0 0 1 1 0 1 1 1 ...
##  - attr(*, ".internal.selfref")=<externalptr>
head(SP500subset, n=12)
##     Week       Year Returns_Lagged_5_Days Average_Daily_Vol._in_Billions
##  1:    1 First Year                -3.484                      0.1549760
##  2:    2 First Year                -0.229                      0.1485740
##  3:    3 First Year                -3.936                      0.1598375
##  4:    4 First Year                 1.572                      0.1616300
##  5:    5 First Year                 0.816                      0.1537280
##  6:    6 First Year                -0.270                      0.1544440
##  7:    7 First Year                -2.576                      0.1517220
##  8:    8 First Year                 3.514                      0.1323100
##  9:    9 First Year                 0.712                      0.1439720
## 10:   10 First Year                 1.178                      0.1336350
## 11:   11 First Year                -1.372                      0.1490240
## 12:   12 First Year                 0.807                      0.1357900
##     Weekly_Returns Direction Previous_4_Day_Trend_Change V8
##  1:         -0.270        -1                           1  1
##  2:         -2.576        -1                          -1  0
##  3:          3.514         1                           1  0
##  4:          0.712         1                           1  0
##  5:          1.178         1                          -1  1
##  6:         -1.372        -1                           1  1
##  7:          0.807         1                           1  0
##  8:          0.041         1                          -1  1
##  9:          1.253         1                          -1  1
## 10:         -2.678        -1                           1  1
## 11:         -1.793        -1                          -1  0
## 12:          2.820         1                          -1  1
SP500subset[5,]
##    Week       Year Returns_Lagged_5_Days Average_Daily_Vol._in_Billions
## 1:    5 First Year                 0.816                       0.153728
##    Weekly_Returns Direction Previous_4_Day_Trend_Change V8
## 1:          1.178         1                          -1  1
SP500subset[70,]
##    Week        Year Returns_Lagged_5_Days Average_Daily_Vol._in_Billions
## 1:   70 Second Year                -1.329                       0.175766
##    Weekly_Returns Direction Previous_4_Day_Trend_Change V8
## 1:          0.754         1                          -1  1
SP500subset[133,]
##    Week       Year Returns_Lagged_5_Days Average_Daily_Vol._in_Billions
## 1:  133 Third Year                -0.967                       0.179974
##    Weekly_Returns Direction Previous_4_Day_Trend_Change V8
## 1:         -0.002        -1                          -1  0
SP500subset[500,]
##    Week       Year Returns_Lagged_5_Days Average_Daily_Vol._in_Billions
## 1:  500 Tenth Year                 -2.14                        0.70362
##    Weekly_Returns Direction Previous_4_Day_Trend_Change V8
## 1:         -0.411        -1                           1  1

Do most days that follow a higher/lower return a day ago than 5 days ago experience a change in that same direction? Only by a small amount. The percentage of days that this happened is 52.9%.