List of Functions

###help(topic) - documentation on topic

help(runif)

###?topic - id

is() - show objects in the search path; specify pat=“pat” to search on a pattern

a <- "troy"
is(a)? #not sure if this is right

dir() - show files in the current directory

getwd() #what working directory i am using
## [1] "/Users/troy/Desktop/RclassJournal"
dir() 
##  [1] "_site.yml"                "CodeOp.html"             
##  [3] "CodeOp.Rmd"               "Data Wrangling.Rmd"      
##  [5] "data_wrangle_assignment"  "Data_Wrangling.html"     
##  [7] "docs"                     "figure"                  
##  [9] "Final Tutorial.Rmd"       "Final_Tutorial_files"    
## [11] "Final_Tutorial.html"      "GGPlot_files"            
## [13] "GGPlot.html"              "GGPlot.Rmd"              
## [15] "histogram_lim_breaks.png" "images"                  
## [17] "index.html"               "index.Rmd"               
## [19] "Intrinsics.Rmd"           "knitr-minimal.md"        
## [21] "LabJournalWebsite.Rproj"  "Links.Rmd"               
## [23] "NOTES.Rmd"                "Power Analysis.Rmd"      
## [25] "README.md"                "Rprof.out"               
## [27] "Shiny_R_Class"            "site_libs"               
## [29] "Statistics.Rmd"           "TroysRBasics_cache"      
## [31] "TroysRBasics_files"       "TroysRBasics.Rmd"        
## [33] "xaringan"

###list.files() - shows files in the directory

list.files()
##  [1] "_site.yml"                "CodeOp.html"             
##  [3] "CodeOp.Rmd"               "Data Wrangling.Rmd"      
##  [5] "data_wrangle_assignment"  "Data_Wrangling.html"     
##  [7] "docs"                     "figure"                  
##  [9] "Final Tutorial.Rmd"       "Final_Tutorial_files"    
## [11] "Final_Tutorial.html"      "GGPlot_files"            
## [13] "GGPlot.html"              "GGPlot.Rmd"              
## [15] "histogram_lim_breaks.png" "images"                  
## [17] "index.html"               "index.Rmd"               
## [19] "Intrinsics.Rmd"           "knitr-minimal.md"        
## [21] "LabJournalWebsite.Rproj"  "Links.Rmd"               
## [23] "NOTES.Rmd"                "Power Analysis.Rmd"      
## [25] "README.md"                "Rprof.out"               
## [27] "Shiny_R_Class"            "site_libs"               
## [29] "Statistics.Rmd"           "TroysRBasics_cache"      
## [31] "TroysRBasics_files"       "TroysRBasics.Rmd"        
## [33] "xaringan"

INPUT AND OUTPUT

###save() - saves the specified objects (…) in the XDR platform independent binary format

save() #i picked files form list but it is saying they arent found 

###load() - load the datasets written with save

load(index.rmd) #says not found

###data() - loads specified data sets

###library() - load add on packages from library

library(bitops) #loading according, box became checked in system library

###read.table() - reads a file in table format and creates a data frame from it; the default separator sep="" is any whitespace; use header=TRUE to read the first line as a header of column names; use as.is=TRUE to prevent character vectors from being converted to factors; use comment.char="" to prevent “#” from being interpreted as a comment; use skip=n to skip n lines before reading data; see the help for options on row naming, NA treatment, and others

#???

###read.csv() - (“filename”,header=TRUE) id. but with defaults set for reading comma-delimited files

myfile <- "troysdata.csv" #the data file i want to read
thedata <- read.csv(myfile, row.names=5)#only read row 5

print(thedata)

###scan() - function read data from screen or file. Large data can be scanned in by just copy and paste, for example paste from EXCEL. Then use “ctrl+v” to paste the data, the data type will be automatically determined.

dir()
##  [1] "_site.yml"                "CodeOp.html"             
##  [3] "CodeOp.Rmd"               "Data Wrangling.Rmd"      
##  [5] "data_wrangle_assignment"  "Data_Wrangling.html"     
##  [7] "docs"                     "figure"                  
##  [9] "Final Tutorial.Rmd"       "Final_Tutorial_files"    
## [11] "Final_Tutorial.html"      "GGPlot_files"            
## [13] "GGPlot.html"              "GGPlot.Rmd"              
## [15] "histogram_lim_breaks.png" "images"                  
## [17] "index.html"               "index.Rmd"               
## [19] "Intrinsics.Rmd"           "knitr-minimal.md"        
## [21] "LabJournalWebsite.Rproj"  "Links.Rmd"               
## [23] "NOTES.Rmd"                "Power Analysis.Rmd"      
## [25] "README.md"                "Rprof.out"               
## [27] "Shiny_R_Class"            "site_libs"               
## [29] "Statistics.Rmd"           "TroysRBasics_cache"      
## [31] "TroysRBasics_files"       "TroysRBasics.Rmd"        
## [33] "xaringan"
scan() #i dont see how this isnt found its clearly in the directory
## numeric(0)

###print() - prints its arguments; generic, meaning it can have different methods for different objects

a <- "hello"
print(a)
## [1] "hello"

###cat() - (…, file="“, sep=” ") prints the arguments after coercing to character; sep is the character separator between arguments

b <- c(1,2,3)
cat(b) #simply shows me whats in variabke (b)
## 1 2 3

###write.table() - prints x after converting to a data frame; if quote is TRUE,character or factor columns are surrounded by quotes ("); sep is the field separator; eol is the end-of-line separator; na is the string for missing values; use col.names=NA to add a blank column header to get the column headers aligned correctly for spreadsheet input

a <- data.frame(a=c(1,2,3),b=c(3,4,5)) #created a table

write.table(a,file="mytable.txt") #gave the file a name

#example list was saved in the notes document but i recently added it to the envirnment as a saved variable

##DATA CREATION

###c() - combine arguements by forming a vector, if recursive=TRUE is used it will descend through lists combining all elements in one vector

first_names <- c("asdasas", "fwefef", "sfweg")

write.table(first_names)
## "x"
## "1" "asdasas"
## "2" "fwefef"
## "3" "sfweg"

###from:to (where from and to are replaced with numbers, e.g. 1:10)

a <- (1:5)
a
## [1] 1 2 3 4 5

###seq() - generates a sequence by= specifies increment, length= specifies how long

seq(2:10, by = 2) # i esentially want to show only even numbers the by= isnt doing what i think it should 

seq(2:10, by = (1))

###rep() - used to replicate. replicate x times; use each= to repeat “each” element of x each times; rep(c(1,2,3),2) is 1 2 3 1 2 3; rep(c(1,2,3),each=2) is 1 1 2 2 3 3

rep(c(1,2,3),each=2)
## [1] 1 1 2 2 3 3

###data.frame - create a data frame of the named or unnamed arguments; data.frame(v=1:4,ch=c(“a”,“B”,“c”,“d”),n=10);shorter vectors are recycled to the length of the longest

###list() - create a list of the named or unnamed arguments

a <- list(a=1,2,3,b="hello",d=2,3,4)
b <- list(a=2, b="hi", d=3)
print(a)
## $a
## [1] 1
## 
## [[2]]
## [1] 2
## 
## [[3]]
## [1] 3
## 
## $b
## [1] "hello"
## 
## $d
## [1] 2
## 
## [[6]]
## [1] 3
## 
## [[7]]
## [1] 4
print(b)
## $a
## [1] 2
## 
## $b
## [1] "hi"
## 
## $d
## [1] 3

###matrix() - build a matrix

h <- matrix(1:20, nrow=5, ncol=5)

View(h)

###factor() - encodes a vector x as a factor

sodium <- factor(c("low", "med", "high"))
levels(sodium)
## [1] "high" "low"  "med"
sodium <- factor(sodium, levels=c("low", "med", "high"), ordered=TRUE)
levels(sodium)
## [1] "low"  "med"  "high"
min(sodium)
## [1] low
## Levels: low < med < high

###rbind() - combine data/info by rows for matrices, data frames, etc. must have equivalent amount of rows, if not equate them by adding NA to either until same qty is reached

###cbind() - combine data by columns same rules apply as rbind

##Slicing and Extracting Data - indexing Vectors ### x[n] - what/remove is in this position in the matrix

a = matrix( 
  c(2, 4, 3, 1, 5, 7), # the data elements 
   nrow=2,              # number of rows 
   ncol=3,              # number of columns 
   byrow = TRUE)        # fill matrix by rows 
 
a                      # print the matrix 
##      [,1] [,2] [,3]
## [1,]    2    4    3
## [2,]    1    5    7
a[2 , 1]   # first thing in second row
## [1] 1

###x[-n] - everything but what is in the [] position

a = matrix( 
  c(2, 4, 3, 1, 5, 7), # the data elements 
   nrow=2,              # number of rows 
   ncol=3,              # number of columns 
   byrow = TRUE)        # fill matrix by rows 
 
a                      # print the matrix 
##      [,1] [,2] [,3]
## [1,]    2    4    3
## [2,]    1    5    7
a[-2, ] # everything but the first thing in the second row
## [1] 2 4 3

x[1:n] what is in positions 1-n

a = matrix( 
  c(2, 4, 3, 1, 5, 7), # the data elements 
   nrow=2,              # number of rows 
   ncol=3,              # number of columns 
   byrow = TRUE)        # fill matrix by rows 
 
a                      # print the matrix 
##      [,1] [,2] [,3]
## [1,]    2    4    3
## [2,]    1    5    7
a[1:2, c(1,1,1,1,1)] # everything but the first thing in the second row
##      [,1] [,2] [,3] [,4] [,5]
## [1,]    2    2    2    2    2
## [2,]    1    1    1    1    1

x[-(1:n)] - elements from n+1 to the end

a = matrix( 
  c(2, 4, 3, 1, 5, 7),  # the data elements 
   nrow=2,              # number of rows 
   ncol=3,              # number of columns 
   byrow = TRUE)        # fill matrix by rows 
 
a [-(1:3)]              # print the matrix 
## [1] 5 3 7

x[c(1,4,2)] - specific elements

a = matrix( 
  c(2, 4, 3, 1, 5, 7),  # the data elements 
   nrow=2,              # number of rows 
   ncol=3,              # number of columns 
   byrow = TRUE)        # fill matrix by rows 

a[c(1,3,5)] #what is in postion 1,3,5
## [1] 2 4 3

x[“name”]

k <- matrix(1:9, nrow = 3 dimnames = list( 
c("row1", "row2"),         # row names 
c("col1", "col2", "col3")) # column names 
 
k      #print A 

 
 # element at 2nd row, 3rd column 

###x[x>3] all elements greater than 3 -

x <- c(1,3,4,5,6,7)
x[x > 3] 
## [1] 4 5 6 7

###x[x > 3 & x < 5] all elements between 3 and 5

x <- c(1,3,4,5,6,7)
x[x > 3 & x < 5] 
## [1] 4

###x[x %in% c(“a”,“and”,“the”)] all elements in given set

##Indexing lists

###x[n] list with elements n

n <- c(2, 3, 5) 
s <- c("aa", "bb", "cc", "dd", "ee")
b <- c(TRUE, FALSE, TRUE, FALSE, FALSE) 
x <- list(n, s, b, 3)   # x contains copies of n, s, b

x[4]
## [[1]]
## [1] 3

###x[[n]] nt h element of the list -

n <- c(2, 3, 5) 
s <- c("aa", "bb", "cc", "dd", "ee")
b <- c(TRUE, FALSE, TRUE, FALSE, FALSE) 
x <- list(n, s, b, 3)   # x contains copies of n, s, b

x[[1]][2]
## [1] 3

###x[[“name”]] element of the list named “name”

b[[TRUE]]
## [1] TRUE

###x$name id.

bob <- c(2, 3, 5)
john <- c("aa", "bb")
v <- list(bob <- c(2, 3, 5), john <- c("aa", "bb")) 
 
v$bob #i expect this to return 2,3,5
## NULL
v[bob]
## [[1]]
## [1] "aa" "bb"
## 
## [[2]]
## NULL
## 
## [[3]]
## NULL

##Indexing matrices

###x[y,b] element at row y, column b

x <- matrix(1:9, nrow = 3, dimnames = list(c("X","Y","Z"), c("A","B","C")))
x
##   A B C
## X 1 4 7
## Y 2 5 8
## Z 3 6 9
x["Y","B"]
## [1] 5

###x[“Z”,] row Z

x["Z",]
## A B C 
## 3 6 9

###x[,c] column

x[,"C"]
## X Y Z 
## 7 8 9

###x[,c(“A”,“C”)] columns A and C

x[,c("A","C")]
##   A C
## X 1 7
## Y 2 8
## Z 3 9

###x[“name”,] row named “name”

x["Y",]
## A B C 
## 2 5 8

##Indexing data frames (matrix indexing plus the following)

###x[[“name”]] column named “name”

x <- matrix(1:9, nrow = 3, dimnames = list(c("X","Y","Z"), c("A","B","C")))
x[["B"]]

###x$named

##Variable conversion ###as.data.frame(x) -

as.data.frame(x, row.names=NULL, optional=FALSE, xy=FALSE, 
              na.rm=FALSE, long=FALSE, ...)#not sure exactly what i am attempting to do here

###as.numeric(x) -

x <- c(1,2,4,5,53,2)
as.numeric(x)# i believe this is should be coding responses as numeric, which i dont quite undetstand in this context.for instance a likert scale 
## [1]  1  2  4  5 53  2

###as.logical(x)

###as.character(x)

##Variable information

###is.na(x)

###is.null(x)

###is.data.frame(x)

###is.numeric(x)

###is.character(x)

###length(x)

x <- c(1,2,5,6,3,6,64,4,3,7,8,9)
length(x)
## [1] 12

###dim(x)

x <- c(1,2,5,6,3,6,64,4,3,7,8,9)
dim(x)#? not sure here
## NULL

###dimnames(x)

###nrow(x)ncol(x) - amount of col/rows, also to use name

matrix(a, nrow = 2, ncol = 3)
##      [,1] [,2] [,3]
## [1,]    2    4    3
## [2,]    1    5    7

###class()

###attributes()

##Data selection and manipulation

###which.max() - returns the largest number

x <- c(1,4,57,54754,6576,3456346,4)

which.max(x) #not sure why its not diagnosing thr enetire number and just the last digit, 
## [1] 6

###which.min() - smallest element of

x <- c(1211,124,57,54754,6576,3456346,4)
which.min(x) #why is this not 4?
## [1] 7

###which() - what postion something is in

y <- c(3,5,7,8,4,3,3,2)
which(y == 4)
## [1] 5

###sort() - puts things in order

m <- c(5,2,6,7,8,3,3,3,3)
sort(m)
## [1] 2 3 3 3 3 5 6 7 8

###unique()

###table() - can be used for frequency tables

d <- c(2,4,5,8,5,3,6,5,8,6,5,3,3,4,6,6,8,8,5,4,3,2,3)
table(d)#, or categorization, assuming i can use strsplit but im not positive
## d
## 2 3 4 5 6 8 
## 2 5 3 5 4 4

###sample - simply put to sample numbers, but you can modify to sample greater or less then and incorporate sort to clean it up

d <- 1:20
sort(sample(d[d > 7]))#sorted all numbers greater than 7 from variable d
##  [1]  8  9 10 11 12 13 14 15 16 17 18 19 20

##MATH

###max() - largest number in variable

max(d)
## [1] 20

###min() - smallest number in variable

min(d)
## [1] 1

###range() - from smallest to largest number

range(d)
## [1]  1 20

###sum - sum of all integers of a given variable

sum(d)
## [1] 210

###mean

mean(d)
## [1] 10.5

###median

median(d)
## [1] 10.5

###var - The average of the squared differences from the Mean

var(d)
## [1] 35

###sd - Standard Deviation is just the square root of Variance

sd(d)
## [1] 5.91608

###cor -

n <- c(2,2,4,4,6,6,6,3,3,3,3)
cor(d,n)#just not sure here abount what to do

###round() - specify whole or decimal numbers in a sequence or within a variable

round(.5 + -2:4)
## [1] -2  0  0  2  2  4  4
( x1 <- seq(-2, 4, by = .5) )#sequence of numbers by the .5 in each sequence 
##  [1] -2.0 -1.5 -1.0 -0.5  0.0  0.5  1.0  1.5  2.0  2.5  3.0  3.5  4.0

###abs() -

abs(d)
##  [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20
x <- matrix(c(-3,5,-7,1,-9,4),nrow=3,ncol=2,byrow=TRUE)
abs(x[1,])#absolute value of row one
## [1] 3 5

##Matrices

###t()

h <- matrix(1:20, nrow=5, ncol=4)
t(h)
##      [,1] [,2] [,3] [,4] [,5]
## [1,]    1    2    3    4    5
## [2,]    6    7    8    9   10
## [3,]   11   12   13   14   15
## [4,]   16   17   18   19   20

###diag() - not sure what this is

# NOT RUN {
dim(diag(3))
## [1] 3 3
diag(10, 3, 4) # guess what?
##      [,1] [,2] [,3] [,4]
## [1,]   10    0    0    0
## [2,]    0   10    0    0
## [3,]    0    0   10    0
all(diag(1:3) == {m <- matrix(0,3,3); diag(m) <- 1:3; m})
## [1] TRUE

###rowsums() / colsums() - sum of rows and columns. rowmeans() / Colmeans()

h <- matrix(1:20, nrow=5, ncol=4)
rowSums(h)
## [1] 34 38 42 46 50
colSums(h)
## [1] 15 40 65 90
rowMeans(h)
## [1]  8.5  9.5 10.5 11.5 12.5
colMeans(h)
## [1]  3  8 13 18

##Advanced data processing ###apply

apply

###aggregate()

#not sure why or what this is going to do for me 

##Strings

###paste()

paste0(1:12) #need an elementary explanation
##  [1] "1"  "2"  "3"  "4"  "5"  "6"  "7"  "8"  "9"  "10" "11" "12"
paste(1:12)      
##  [1] "1"  "2"  "3"  "4"  "5"  "6"  "7"  "8"  "9"  "10" "11" "12"
as.character(1:12)
##  [1] "1"  "2"  "3"  "4"  "5"  "6"  "7"  "8"  "9"  "10" "11" "12"

###strsplit - strsplit helps find letters in string

q <- "dogs cats" #strsplit helps find letters in string
strsplit(q,split="") #to split a character vector
## [[1]]
## [1] "d" "o" "g" "s" " " "c" "a" "t" "s"
t <-strsplit(q,split ="")



t[[1]]
## [1] "d" "o" "g" "s" " " "c" "a" "t" "s"
t[[1]][2]#use to access second item in string
## [1] "o"
t[[1]][6]#use to access sixth item in string
## [1] "c"
table(t)
## t
##   a c d g o s t 
## 1 1 1 1 1 1 2 1

###tolower() -

test1 <- c(text1 = "England and France are members of NATO and UNESCO", 
text2 = "NASA sent a rocket into space.")
tolower(test1)
##                                               text1 
## "england and france are members of nato and unesco" 
##                                               text2 
##                    "nasa sent a rocket into space."

###toupper() -

test1 <- c(text1 = "England and France are members of NATO and UNESCO", 
text2 = "NASA sent a rocket into space.")
toupper(test1)
##                                               text1 
## "ENGLAND AND FRANCE ARE MEMBERS OF NATO AND UNESCO" 
##                                               text2 
##                    "NASA SENT A ROCKET INTO SPACE."

##Plotting

###hist()

v <- c(3, 4, 7, 6, 5, 1, 4, 3, 4, 4, 5, 4, 0, 3, 5, 4, 4, 4)

# Give the chart file a name.
png(file = "histogram_lim_breaks.png")

# Create the histogram.
hist(v,xlab = "Weight",col = "green",border = "red", xlim = c(0,8), ylim = c(0,10),
   breaks = 5)

###plot()

v <- c(3, 4, 7, 6, 5, 1, 4, 3, 4, 4, 5, 4, 0, 3, 5, 4, 4, 4)

# Give the chart file a name.
png(file = "histogram_lim_breaks.png")

# Create the histogram.
plot(v,xlab = "Weight",col = "green", xlim = c(0,8), ylim = c(0,10))

##Distributions

###rnorm - generates random deviates.

rnorm(n, mean = 10, sd = 1.5)
## [1]  8.339255 10.111573  9.808169

###runif - random numbers

n <- c(1,2,5,7,8,4,3,2,5,6,8,8,5)
runif(n, min = 0, max = 10)
##  [1] 9.5623557 8.7015802 3.1562935 7.9581245 0.4789979 8.9265102 9.5411924
##  [8] 6.2046301 0.8220830 9.5295412 2.6235542 7.0328795 4.2676212

##Programming

###show that you can define a function

addtwo <- function(x) {
  b <- x + 2
  return(b)
}
addtwo(2) 
## [1] 4
sumsq <- function(x,y) {
  x^2 + y^2
}
sumsq(2,3)#i think i need to insert a table to truly make this work 
## [1] 13

###show that you can write a for loop

###show that you can write a while loop

x <- 4
while(x < 100) {
  x <- x + 8

print(x)
}
## [1] 12
## [1] 20
## [1] 28
## [1] 36
## [1] 44
## [1] 52
## [1] 60
## [1] 68
## [1] 76
## [1] 84
## [1] 92
## [1] 100

###show that you can write an if else statement

j = c(1:30)
ifelse(j %% 3 == 0, "three", "not")
##  [1] "not"   "not"   "three" "not"   "not"   "three" "not"   "not"  
##  [9] "three" "not"   "not"   "three" "not"   "not"   "three" "not"  
## [17] "not"   "three" "not"   "not"   "three" "not"   "not"   "three"
## [25] "not"   "not"   "three" "not"   "not"   "three"

###Explain what return() does inside a function, show you can use it

check <- function(x) {
if (x > 0) {
result <- "Positive"
}
else if (x < 0) {
result <- "Negative"
}
else {
result <- "Zero"
}
return(result)
}
check(1)
## [1] "Positive"
check(-3)
## [1] "Negative"
check(0)
## [1] "Zero"

###Explain what break() does, show you can use it