# Name First R Program # Purpose is to install and show basic R commands # Author Sunil Gupta, SASSavvy.com/R-Guru # Date Oct 25 2021 # Copyright - 2021, This macro has copyright protection by the author. Application of this program for its intended use only in its original form is authorized by the author. All other applications are at users risk. R program is not to be used in commercial software product without the written approval by the author. Any reference to the R program must include reference to SASSavvy.com/R-Guru. No support or guarantee is provided with the R program or results. User takes responsibility for R program and results and is encouraged to test the program. # 1. install R windows (32 or 64 bit) # https://cran.rstudio.com # 2. download R studio # https://www.rstudio.com/products/rstudio/download # 3. Double click on R icon # 4. Please select a CRAN mirror for use in this session # Select US option for internal setting. setwd("C:/study/analysis/output") # change default working folder getwd() # confirm working folder # option b install.packages('tidyverse') library(tidyverse) # load popular data management package library(dplyr) # load common data management and sql package library(sqldf) # load sql package library() # display loaded libraries .libPaths() # display path of each package lsf.str("package:dplyr") # display dplyr functions lsf.str("package:tidyverse") # display tidyverse functions t <- c("Sun","Mon","Tue","Wed","Thurs","Fri","Sat") view(t) x= c(2, 4, 6) mean(x) y=c(1, 1, 1) z = x + y view(z) mydata <- data.frame( class = c("1st", "2nd", "3rd", "Crew"), n = c(325, 285, 706, 885), prop = c(14.8, 12.9, 32.1, 40.2) ) mydata tg <- ToothGrowth # save sample data frame to tg data frame View(tg) # browse tg str(tg) # display tg attributes and sample records attributes(tg) # display tg attributes, names (tg) is alternative to display variable names head(tg) # display tg sample records print(tg) # display tg all records, similar to proc print summary(tg) # display stats object of continuous variables table(tg) # display freq of categorical variables objects() # to display all objects in workspace (data frames, vectors, etc.) library(readxl) # read excel files dm_specs <- read_excel("D:/Sunil/GP/Business/ACL Digital/r programming/DM_Specs.xlsx") View(dm_specs) dm <- read.csv("D:/Sunil/GP/Business/ACL Digital/r programming/input.csv") dm1 <- read.table("D:/Sunil/GP/Business/ACL Digital/r programming/input.csv") dm dm1 write.csv(dm, "C:/study/analysis/output/mydm.csv", row.names = FALSE) # save permanent csv sdtm <- "D:/Sunil/GP/Business/ACL Digital/r programming" # assign libname to object named sdtm list.files dir library(haven) # read SAS datasets asl1 <- read_sas(file.path(sdtm, 'adsl.sas7bdat')) # read_sas from haven package within tidyverse to read DM dataset in sdtm library asl1 asl2 <- read_sas("D:/Sunil/GP/Business/ACL Digital/r programming/adsl.sas7bdat") # read DM dataset directly asl2 View(asl2) saveRDS(asl2, file = "asl2.RDS") # save permanent R data frame myasl <- readRDS("asl2.RDS") # load permanent R data frame myasl # enter data values into data frame vs <- read.table(header = TRUE, stringsAsFactors = FALSE, text = " SUBJID HT WT PERIOD 001 180 NA 1 001 181 77 2 002 173 85 1 002 173 83 2"); View(vs) test_df <- data.frame( class = c("1st", "2nd", "3rd", "Crew"), n = c(325, 285, 706, 885), prop = c(14.8, 12.9, 32.1, 40.2) ) View(test_df) patient <- 1 # assign number patient = 1 is(patient) patient Right Variable Assignment 1 -> patient # right assign number patient = 1 is(patient) patient # Variable Assignment Options assign('ae', 'Headache') # assign char ae = 'Headache' ae = 'Headache' # assign char ae = 'Headache' ae # Keeping Variables test_df2=test_df[c('class', 'n')] View(test_df2) # Dropping Variables test_df3= subset(test_df, select = -c(class)) View(test_df3) test_df4= test_df[-c(3)] View(test_df4) df1 <- subset(test_df, class == '1st') df1 <- filter(test_df, class == '1st') df1 # sorting examples using the mtcars dataset attach(mtcars) # sort by mpg newdata <- mtcars[order(mpg),] # sort by mpg and cyl newdata <- mtcars[order(mpg, cyl),] newdata # pipe example query1 <- ToothGrowth %>% select(len, supp, dose) %>% mutate(dose2 = (dose*2)) %>% filter(supp == 'VC') %>% arrange(supp, dose) View(query1) # intermediate objects query1 <- ToothGrowth query2 <- select(query1, len, supp, dose) View(query2) query3 <- mutate(query2, dose2 = (dose*2)) View(query3) query4 <- filter(query3, supp == 'VC') View(query4) query5 <- arrange(query4, supp, dose) View(query5) objects() ASL_mean <- asl1 %>% group_by(SEX) %>% summarise(avg_age = mean(AGE)) asl1 ASL_mean table(asl1$SEX) # proc freq data=asl1; tables sex; run; table(asl1$SEX, asl1$SAFFL) # proc freq data=asl1; tables sex*saffl; run;