Load packages

If you are using the package for the first time, you will first have to install it.

# install.packages("survival") 
# install.packages("openxlsx") 

If you have already downloaded this package in the current version of R, you will only have to load the package.

library(survival)
library(openxlsx)

Get the data

Load a data set from a package.
You can use the double colon symbol (:), to return the pbc object from the package survival. We store this data set to an object with the name pbc.

pbc <- survival::pbc

List all your R objects

ls()
##   [1] "a"                   "A"                   "Age"                 "age_time"            "age_time_sex"       
##   [6] "AgeCat"              "ar"                  "ar1"                 "b"                   "B"                  
##  [11] "basedat"             "birthwt"             "C"                   "cal_sd"              "cal_var"            
##  [16] "Columns_com"         "cSum"                "dat"                 "dat1"                "dat2"               
##  [21] "dat3"                "dat4"                "dataset1"            "dataset2"            "datlist"            
##  [26] "decimals"            "demos"               "DerivativeFunction"  "des"                 "DF"                 
##  [31] "df_test"             "df1_test"            "df2_test"            "dice"                "die"                
##  [36] "dt"                  "dt_events"           "esoph"               "exdat"               "fam_lm"             
##  [41] "files"               "fm1"                 "fm2"                 "fmla1"               "fmla2"              
##  [46] "fmla3"               "fmla4"               "fmla5"               "FUdat"               "fun1"               
##  [51] "Fun1"                "fun2"                "fun3"                "fun4"                "fun4_correct"       
##  [56] "fun5"                "fun5b"               "fun6"                "fun7"                "Function2"          
##  [61] "google.trends1"      "h"                   "heart"               "hi"                  "hint"               
##  [66] "html_files"          "htmlfiles"           "i"                   "img"                 "j"                  
##  [71] "k"                   "labdat"              "let"                 "list_pbc"            "list_test"          
##  [76] "list1"               "list2"               "lung"                "M"                   "marks"              
##  [81] "mat"                 "Mat"                 "mat_test"            "mdat"                "mdat_all"           
##  [86] "mdat_x"              "mdat_y"              "mdat3"               "means"               "MHtest"             
##  [91] "mod"                 "mod_chol"            "mod_chol2"           "mod_lm"              "mod_sub"            
##  [96] "mod1"                "mod1b"               "mod1c"               "mod2"                "mod2a"              
## [101] "mod2b"               "mod2c"               "mod3"                "my_list"             "mylist"             
## [106] "myList"              "MyList"              "mysummary"           "N"                   "newData"            
## [111] "newList"             "nrj"                 "number"              "otherlist"           "out"                
## [116] "output"              "p"                   "p_adj_BH"            "p_adj_Bonf"          "p4"                 
## [121] "padj"                "patient"             "pbc"                 "pbc_chol_na"         "pbc_female_bili"    
## [126] "pbc_male_bili"       "pbc_males"           "pbc_out_bili"        "pbc_sub"             "pbcLong"            
## [131] "pbcseq"              "pbcseq.idNEW2"       "pbcseq.idNEW3"       "pbcseqWide"          "pdfs"               
## [136] "plot_summary"        "plotdat"             "practicals"          "props"               "pv"                 
## [141] "pval1"               "pval2"               "pval3"               "pvals"               "res"                
## [146] "res1"                "res2"                "res3"                "results"             "retinopathy"        
## [151] "Rfiles"              "Rmd_files"           "Rmdfiles"            "scalar_test"         "sex"                
## [156] "Sex"                 "shinyFiles"          "slides"              "smod_chol"           "smod1"              
## [161] "smod1c"              "smod2"               "static"              "std_num"             "stratum"            
## [166] "subset_data"         "summary_categorical" "summary_continuous"  "summary_df"          "summary_list"       
## [171] "summary_lm"          "survey"              "tab"                 "tab1"                "test_lm"            
## [176] "test_logit"          "test_probit"         "testdat"             "testres"             "textdat"            
## [181] "Treatment"           "ttest1"              "ttest2"              "vec"                 "vec_test"           
## [186] "vec1"                "vec1_test"           "vec2"                "vec2_test"           "vec3"               
## [191] "vec4"                "weight"              "Weight"              "write_Demos_md"      "write_Practicals_md"
## [196] "write_Slides_md"     "wtest"               "x"                   "X"                   "x1"                 
## [201] "x2"                  "xnew"                "xvec"                "xxx"                 "y"                  
## [206] "Y"                   "y1"                  "y2"                  "z"

Save your work

Take care: first you need to set your working directory (Rstudio: Session -> Set Working Directory -> Choose Directory…). Otherwise you do not know where your R workspace is saved.
You can also the function setwd(...) to set the working directory

getwd()
## [1] "C:/Users/erler/Documents/Work/Projects/Teaching/BST02/Demos/Basic use of R"
dt <- pbc[1:6, c("id", "sex", "bili", "chol")]
p <- 1

#save(dt, p, file = "data.RData")
#saveRDS(dt, file = "data1.RData")

Load previous work

#load("data.RData")
#readRDS("data1.RData")

Transform a RData data set into xls, csv or text format

#write.csv(dt, "mydata.csv")
#write.table(dt, "mydata.txt", sep="\t")
#write.xlsx(dt, "mydata.xlsx")