# book chapter introduction to R code # chapter page 1 myresult <- 10 # this is all a comment myresult # section 2 Showing the value and structure of an object str() # str() = structure NOT string as in some other programming languages str(myresult) # chapter page 2 section 3 sum1 <- 2 + 6 + 3 sum1 sum2 <- 2 + 6 + 3 sum2 sum3 <- 2 + 6 + + 3 sum3 sum4 <- 2 + 6 + 3 sum4 ### myresult2 <- 20; myresult3 <- 50 ; myresult4 <- 100 ; myresult5 <- 700 #### section 5 joining individual values c() x<- c(71, 68, 68, 66, 67, 70, 71, 70, 73, 72, 65, 66) y <- c(69, 64, 65, 63, 65, 62, 65, 64, 66, 59, 62) x; y x[2] x <- x[-2] x x_time2 <- 2 * x x_time2 # obtain summary stats. summary(x) summary(y) # section 6 dataframes and lists mydataframe=data.frame(x_axis=x, y_axis=y) mydataframe summary(mydataframe) names(mydataframe) # returns names of columns mydataframe$y_axis # returns the values for the y_axis column mydataframe[1] # returns the values for the first column, same as mydataframe[,1] mydataframe[1,] # returns the values for the first row ######### factor creation and manipulation # factor consists of element each has a internal value (numeric) # and a optional label (character) # in factor() function you specify the labels, internal values assigned automatically # and sequentially starting at 1. # internal values still 1's and 2's in all three below: # example 1 grouping_var <- factor(c(1,1,1,1,2,2,2,2)) grouping_var str(grouping_var) # structure function # example 2 grouping_string <- factor(c("one","one","one","one","two","two","two","two")) grouping_string str(grouping_string) # example 3 grouping_gap <- factor(c(5,5,5,5,10,10,10,10)) grouping_gap str(grouping_gap) # using gl() to create factors: grouping_var2 <- gl(2,8, labels= c("control", "treat")) grouping_var2 # note that just requesting the factor name # produces the list of labels applies to each of the interal values: # i.e. grouping_var2 # [1] control control control control control control control control treat #[10] treat treat treat treat treat treat treat # Levels: control treat #extraction # extracting internal values easy as already numeric numeric1<- as.numeric(grouping_var) numeric1 numeric12<- as.numeric(grouping_string) numeric12 numeric13<- as.numeric(grouping_gap) numeric13 # check they are numeric is.numeric(numeric13) str(numeric13) # extracting labels need to convert: # if labels do not look like numbers just get NA's # na example: as.numeric(levels(grouping_var2))[as.integer(grouping_var2)] # working example: as.numeric(levels(grouping_gap))[as.integer(grouping_gap)] #################### values<- as.numeric(as.character(grouping_var2)) values as.numeric(levels(grouping_var2))[as.integer(grouping_var2)] as.numeric(levels(f))[as.integer(f)] ################# # section 8 editing a dataframe variable1<- c(2,5,7) variable2<- c(24,36,20) mydataframe2 <- data.frame(happiness=variable1, age=variable2) mydataframe2 mydataframe2[1] # returns the values for the first column, mydataframe2[,1] # same as above mydataframe2[1,] # row mydataframe2 mydataframe2[2, ] <- 44 # change all values in second row to 44 mydataframe2 mydataframe2[ ,1] <- 100 # change all values in first column to 100 mydataframe2 # change value in first column first row to 3 mydataframe2[ 1,1] <- 3 mydataframe2 ####################### # column actions # changing the name for the first column names (mydataframe2) [1] <- "var_renamed" # create three new columns for the dataframe mydataframe2["newvar1"] <- 1 mydataframe2["newvar2"] <- 0 mydataframe2["newvar3"] <- 50 mydataframe2 # remove the newvar2 variable mydataframe2["newvar2"] <- NULL mydataframe2 ################## # section 9 Creating a new dataframe from a subset of columns # Creating new dataframe from subset of columns # select only two columns called "age" and "newvar3" # to form a new dataframe called newdataframe_subset, newdataframe_subset<- mydataframe2[, c("age", "newvar3")] newdataframe_subset ################## # section 10 Creating new dataframe from subset of rows # can select specific rows based upon some criteria. # Say we wanted to only select rows where the var_renamed vector only has a value of 100. newdataframe_subrows <- mydataframe2[mydataframe2$"var_renamed" ==100, ] newdataframe_subrows ################## # section 11 Loading data and giving it focus - attach() mydataframe <- read.delim("http://www.robin-beaumont.co.uk/virtualclassroom/stats/basics/coursework/data/pain_medication.dat", header=TRUE) # In contrast, if you have a local copy # you need to replace the forward slashes '/' with double back slashes '\\' thus: mydataframe <- read.delim("D:\\folder\\folder\\pain_medication.dat", header=TRUE) # If you can't remember the place or name where you have the # file you can annotate the above R code to: mydataframe <- read.table(file=file.choose()) ################## # section 12 Seeing and Setting the Working Directory # seeing: getwd() # returns [1] "C:/Users/robin/Documents" # setting: setwd("D:/r scripts") #### .Rprofile options # see; http://www.dummies.com/how-to/content/how-to-configure-r.html # see; http://stackoverflow.com/questions/5085774/how-to-modify-and-save-rprofile-site-under-windows # To Set the working directory in R permanently see; http://faculty.chicagobooth.edu/robert.gramacy/teaching/ara/setcwd.pdf # 1. Open the Rprofile.site file on text editor (notepad) # 2. Add the line: setwd("") to the end of theRprofile.site file and add a new line (i.e. hit the enter key) # example from above pdf: # Things you might want to change # options(papersize="a4") # options(editor="notepad") # options(pager="internal") # set the default help type # options(help_type="text") options(help_type="html") # set a site library # .Library.site <- file.path(chartr("\\", "/", R.home()), "site-library") # set a CRAN mirror # local({r <- getOption("repos") # r["CRAN"] <- "http://my.local.cran" # options(repos=r)}) setwd("C:\\Programming\\R\\Data\\RegressionClassData") # NOTICE THE EXTRA EMPTY LINE AFTER THE setwd() COMMAND. ################## # section 13 Saving and loading your data in R format # saving: save(mydataframe,file="mydata.rda") # To load the dataframe use the load() function: load("mydata.Rda") ################## # section 14 Saving your data as a text file # To write a tab delimited file, with the names of each column at the top: write.table(mydataframe,"d:/basics/coursework/data/pain_medication.dat", header=TRUE, sep = "\t") # To write a comma separated file with the names of each column at the top: write.csv(mydataframe, "d:/basics/coursework/data/pain_medication.dat", header=TRUE)