Run R Programs to Create Objects

R is an interpreted language so your R code is directly executed instead of being compiled into an object language.  R is different from SAS in that there are five different essential R data objects and structures – vectors, matrix, array, data frames and lists.  For SAS programmers, vectors and data frames are the most important. As a basic concept, vectors are similar to values in a dataset with only one variable and data frames contain a collection of vectors so contain many variables and records.  Most all R processing is performed on data frames.  All R data structures and any outputs created from R functions are R objects.  R List objects enable processing or looping through a list of data frames, variables or values.  R List objects are similar to processing arrarys in SAS. 

Matrix Applications
  • Variables are generally few and store numbers
  • Primarily for math operations: addition, subtraction, multiplication and division


    • subsets may create vector instead of data frame
    • can search by partial variable name
    • tidy (tibble, tribble) YouTube Differences 1, 2, Blog
    • tibble()
      • created from data frame when using tidyverse, dplyr and ggplot2 packages
      • special type of data frame
      • does not have row names
      • create similar to data.frame() which is in row format
      • variable type displayed in view
      • print displays first few rows and n= option instead of all rows
      • requires same number of records for each variable
      • subsets always result in data frames

    • tribble()
      • tribble created by data in column format instead of in row format 

Within RStudio, submitting R commands is a simple process. Once an R script file is opened, you just select one or more lines and enter Control-Enter to execute them. The alternative is to select Code from the menu and then Run Line or Selection or Run All. The cursor can be placed anywhere on the R command to execute it. Note that if partial R command is submitted, then R will insert ‘+’ in the log to recognize partial R commands and will then expect the remaining R command. See the log window for results after each R comment.

The R script is essentially a text file with an .R extension. The .R extension enables R to recognize it. A tip is to copy R comments from a text file into an R script file. First set the folder to your R program, setwd('/home/username/desktop/rcode') and then enter source('analyze.r').  Best practices are to create an R setup file to install packages and load libraries used on regular tasks similar to SASAUTOS.

Within base R, you can run R statements individually from the Edit > Run Line or Selection option or run all R statements in an R script file from the Edit > Run All option.  Results will be displayed in the console window and the tg data frame window will open.

For maximum viewing of R program and log, select Windows > Tile Horizontally.

Powered by Wild Apricot Membership Software