#Creating logical Variable
aaa <- TRUE
#creating numerical variables
one <- c(1)
four <- c(4)
#create different vectors
## Remember character vectors need "" but not numerical
## Needs the word vector to create
vec_1 <- c(1,2,3,4,5,6,7,8,9,10)
#Has to be an easier way to do this, ask how.
vec_2 <- c(1*3,2*3,3*3,4*3,5*3,6*3,7*3,8*3,9*3,10*3)
vec_3 <- c(vec_1*vec_2)
# Ask how to delete individual elements in environment, Changed name to remove "_vec"
vec_4 <- c(1,2,3,4,5,6,7,8,9,10,11,12)
#Creating matrices
mat_1 <- matrix(1:12, byrow=TRUE, nrow=4)
mat_1
## [,1] [,2] [,3]
## [1,] 1 2 3
## [2,] 4 5 6
## [3,] 7 8 9
## [4,] 10 11 12
# how does it know I want vector_4 besides 1:12
# Seems like R gets confused when I change names, do I need to restart it?
## Sweeping the environment and running all seems to help
# Where is Run All besides ctrl + Shift + enter
mat_2 <- matrix(1:12, byrow=FALSE, nrow=3)
mat_2
## [,1] [,2] [,3] [,4]
## [1,] 1 4 7 10
## [2,] 2 5 8 11
## [3,] 3 6 9 12
#Creating data frame
name <- c("Mercury", "Venus", "Earth",
"Mars", "Jupiter", "Saturn",
"Uranus", "Neptune")
type <- c("Terrestrial planet",
"Terrestrial planet",
"Terrestrial planet",
"Terrestrial planet", "Gas giant",
"Gas giant", "Gas giant", "Gas giant")
diameter <- c(0.382, 0.949, 1, 0.532,
11.209, 9.449, 4.007, 3.883)
rotation <- c(58.64, -243.02, 1, 1.03,
0.41, 0.43, -0.72, 0.67)
rings <- c(FALSE, FALSE, FALSE, FALSE, TRUE, TRUE, TRUE, TRUE)
#Date Frames dont need "" when declaring vectors
planets_df <- data.frame(name, type, diameter, rotation, rings)
planets_df
## name type diameter rotation rings
## 1 Mercury Terrestrial planet 0.382 58.64 FALSE
## 2 Venus Terrestrial planet 0.949 -243.02 FALSE
## 3 Earth Terrestrial planet 1.000 1.00 FALSE
## 4 Mars Terrestrial planet 0.532 1.03 FALSE
## 5 Jupiter Gas giant 11.209 0.41 TRUE
## 6 Saturn Gas giant 9.449 0.43 TRUE
## 7 Uranus Gas giant 4.007 -0.72 TRUE
## 8 Neptune Gas giant 3.883 0.67 TRUE
rot <- planets_df[1:8,"rotation"]
rot
## [1] 58.64 -243.02 1.00 1.03 0.41 0.43 -0.72 0.67
var_1 <- planets_df[2,3]
#Hopefully you mean the single value of Venus' diameter not all of row 2 and all of column 3
var_1
## [1] 0.949
# Extract even rows from matrix
## Have to create "dummy indicator" need explanation about %% 2 and ==0
row_even <- seq_len(nrow(planets_df)) %% 2
row_even
## [1] 1 0 1 0 1 0 1 0
planets_2 <- planets_df[row_even == 0, ]
planets_2
## name type diameter rotation rings
## 2 Venus Terrestrial planet 0.949 -243.02 FALSE
## 4 Mars Terrestrial planet 0.532 1.03 FALSE
## 6 Saturn Gas giant 9.449 0.43 TRUE
## 8 Neptune Gas giant 3.883 0.67 TRUE