Introduction to R
Using a scriptable language, like R, and software that makes life a little easier, like R studio, are great ways to create replicable and shareable sets of analyses.
So let’s begin with a few basic ideas about how R works. The fundamental worker is the function, which is always followed by the open and close parantheses. Each function executes an operation as designated by the series of arguments, which are set by values in the parentheses. Arguments can tell the function which data to use, the methods (one-tail vs. two-tail), and many other options. Some functions are more complicated that others and require many arguments, while other functions are similar and may require only one or two arguments. Arguments can be set by using =, and some funtions will have default values for arguments and others require specific arguments to be referenced.
Data structures (not necessarily the dataset you are analyzing) in R can be in several forms, and we will begin with vectors. Vectors are sets of n elements, and you can generate them by using the function c() and listing individual numbers separated by commas. In this function, the arguments are the individual elements that need to be combined. If you need to generate a sequence of numbers, use the function seq() or if the numerals are separated by 1, you can use the colon; if you need to repeat something, the rep() function is the way to go. You can mix and match some of the functions, too.
c(1,2,3,4)
## [1] 1 2 3 4
seq(1,4, by=1)
## [1] 1 2 3 4
c(1:4)
## [1] 1 2 3 4
rep(seq(1,4, by=1), 2)
## [1] 1 2 3 4 1 2 3 4
Often, our datasets are not stored as vectors of data, but as matrices or dataframes. A matrix contains (n m) numeric elements, where n is the number of rows and m is the number of columns. Functions like rbind() and cbind() can be used to glue vectors and matrices together, based on rows or columns. A dataframe also contains (n m) elements, but these can be stored as numeric and/or other data types besides numeric (e.g. factor). Notice that the R displays matrices different than dataframes.matrix(rep(seq(1,4, by=1),2), nrow=4)
## [,1] [,2]
## [1,] 1 1
## [2,] 2 2
## [3,] 3 3
## [4,] 4 4
cbind(c(1,2,3,4), seq(1,4, by=1)) #This is still stored as a matrix
## [,1] [,2]
## [1,] 1 1
## [2,] |
2 |
2 |
## [3,] |
3 |
3 |
## [4,] |
4 |
4 |
rbind(c(1,2,3,4), seq(1,4, by=1)) #This is still a matrix
## [,1] [,2] [,3] [,4]
## [1,] 1 2 3 4
## [2,] 1 2 3 4
data.frame(cbind(c(1,2,3,4), seq(1,4, by=1)))
## X1 X2 ## 1 1 1
## 2 2 2
## 3 3 3
## 4 4 4
Another datastructure that might useful is an array, which is similar to a matrix, but has more than two dimensions.
array(cbind(c(1,2,3,4), seq(1,4, by=1)),dim=c(3,2,2))
## , , 1 ##
## [,1] [,2]
## [1,] 1 4
## [2,] 2 1
## [3,] 3 2 ##
## , , 2 ##
## [,1] [,2]
## [1,] 3 2
## [2,] 4 3
## [3,] 1 4
CS 340 Milestone One Guidelines and Rubric Overview: For this assignment, you will implement the fundamental operations of create, read, update,
Retail Transaction Programming Project Project Requirements: Develop a program to emulate a purchase transaction at a retail store. This
7COM1028 Secure Systems Programming Referral Coursework: Secure
Create a GUI program that:Accepts the following from a user:Item NameItem QuantityItem PriceAllows the user to create a file to store the sales receip
CS 340 Final Project Guidelines and Rubric Overview The final project will encompass developing a web service using a software stack and impleme