# ------------------------------------------------------------------------------
# How to use variables and Data Frames in R
# ------------------------------------------------------------------------------
# integer
i <- 23; print(i)
# double
d <- 2.3; print(d)
# string
s <- 'hello world'; print(s)
# boolean
b <- TRUE; print(b)
# create a list
a <- list(a1=1, a2=2, a3=3)
print(a); print(a$a1);print(a$a2);print(a$a3)
# add a named item to a list
a$a4=4; print(a); print(a$a4)
# create a vector using the c() function
v <- c(98, 99, 100)
print(v); print(v[1:2])
# create a vector from a range of integers
r <- (1:10)
print(r); print(r[5:10])
# add a new item to the end of a vector
v <- c(1, 2, 3); print(v)
v[4] <- 4; print(v)
# Create a 4-row, 3-column matrix with named headings
data <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)
headings <- list(NULL, c("a","b","c"))
m <- matrix(data, nrow=4, ncol=3, byrow=TRUE, dimnames=headings)
print(m)
print(m[3,])
print(m[,2])
# create a new data frame
years <- c(1980, 1985, 1990)
scores <- c(34, 44, 83)
df <- data.frame(years, scores)
dim(df); print(df)
print(df[,1]); print(df$years)