Create vectors with presidents names and, winner or opponent from the presidents’ height data
presid_name= c("Obama","Bush","Bush","Clinton","Clinton","Bush Father","Reagan","Reagan","Carter","Nixon","Nixon","Johnson","Kennedy","Eisenhower","Eisenhower","Truman")
winner = c(185, 182, 182, 188, 188, 188, 185, 185, 177, 182, 182, 193, 183, 179, 179, 175)
opponent = c(175, 193, 185, 187, 188, 173, 180, 177, 183, 185, 180, 180, 182, 178, 178, 173)
create another vector to store the election year
year= seq (from= 2008, to= 1948, by=-4)
Vector creaded, and called “isWinnerTaller”, which is True when Winner > Opponent and False otherwise
isWinnerTaller= winner > opponent
Show the new vectoe
isWinnerTaller
[1] TRUE FALSE FALSE TRUE FALSE TRUE TRUE TRUE FALSE FALSE TRUE TRUE TRUE TRUE TRUE TRUE
Count how many times the winner has been taller than the opponent.
sum(isWinnerTaller)
[1] 11
Create a Data Frame unifying the vectors created before and we show the dataframe
df_presidents= data.frame(presid_name, year, winner, opponent, isWinnerTaller)
df_presidents
Show the first 6 of the data frame
head(df_presidents)
Display the internal structure of the data frame
str (df_presidents)
'data.frame': 16 obs. of 5 variables:
$ presid_name : chr "Obama" "Bush" "Bush" "Clinton" ...
$ year : num 2008 2004 2000 1996 1992 ...
$ winner : num 185 182 182 188 188 188 185 185 177 182 ...
$ opponent : num 175 193 185 187 188 173 180 177 183 185 ...
$ isWinnerTaller: logi TRUE FALSE FALSE TRUE FALSE TRUE ...
Show column names
colnames(df_presidents)
[1] "presid_name" "year" "winner" "opponent" "isWinnerTaller"
Show number of columns
ncol(df_presidents)
[1] 5
Show number of Rows
nrow(df_presidents)
[1] 16
Add a column named difference as the last column
df_presidents$difference = winner - opponent
Show the new dataset wiht the new column added
df_presidents
Delete the difference column by using its index.
df_presidents [ , -6]
Add the column difference again, so we can delete it again but using a different method.
df_presidents$difference = winner - opponent
The right way is to re-assign the deletion to the data frame
df_presidents = df_presidents [, -6]
Add the column difference again, so we can delete it one more time using a different method.
df_presidents$difference = winner - opponent
Column deleted by using its name
df_presidents= df_presidents[ , colnames(df_presidents) != 'difference' ]
Get the second column
df_presidents[ , 2]
[1] 2008 2004 2000 1996 1992 1988 1984 1980 1976 1972 1968 1964 1960 1956 1952 1948
Get the last column
df_presidents[ , ncol(df_presidents)]
[1] TRUE FALSE FALSE TRUE FALSE TRUE TRUE TRUE FALSE FALSE TRUE TRUE TRUE TRUE TRUE TRUE
Get the winner’s and the opponent’s heights, only for the first three rows in the data set.
df_presidents [c(1,2,3), c(3,4)]
Example: Use the subset() function to get the rows from the data frame where the Winner > Opponent
subset (df_presidents, df_presidents$isWinnerTaller==TRUE)
Get the winners’ names only for cases when Winner > Opponent
subset (df_presidents$presid_name, df_presidents$isWinnerTaller==TRUE)
[1] "Obama" "Clinton" "Bush Father" "Reagan" "Reagan" "Nixon" "Johnson" "Kennedy" "Eisenhower" "Eisenhower" "Truman"
Add a column with the party of the winner.
party= c("Dem", "Rep", "Rep", "Dem", "Dem", "Rep", "Rep", "Rep", "Dem","Rep","Rep","Dem","Dem","Rep","Rep","Dem")
df_presidents$presid_party= party
df_presidents
Use tapply() to compute the mean height for the presidents from each party
tapply (df_presidents$winner, df_presidents$presid_party, mean)
Dem Rep
184.1429 182.6667
Use tapply() to compute the max height for the presidents from each party
tapply (df_presidents$winner, df_presidents$presid_party, max)
Dem Rep
193 188
Use apply() to compute the mean height for the winners and opponents
apply (df_presidents [, c("winner", "opponent")] ,2, mean)
winner opponent
183.3125 181.0625
We can also use the colMeans () function
colMeans (df_presidents [, c("winner", "opponent")])
winner opponent
183.3125 181.0625
Use apply() to compute the sd of the height for the winners and opponents
apply (df_presidents [, c("winner", "opponent")] , 2, sd)
winner opponent
4.629165 5.579352
sd(df_presidents [, "winner"])
[1] 4.629165
sd(df_presidents [, "opponent"])
[1] 5.579352