Directory setting

#check the present working directory (do not mix with the linux command-pwd)
getwd()
## [1] "D:/Rworkshop"
#change the working directory  (do not mix with the linux command-cd)
setwd("D:/Rworkshop")
#create a directory
dir.create("D:/Rworkshop/test/")
## Warning in dir.create("D:/Rworkshop/test/"): 'D:\Rworkshop\test'已存在
dir.exists("D:/Rworkshop/test/")
## [1] TRUE

Notice that dir.create() can only create one directory per time.
setwd() cannot set a directory that doesn’t exist.
Tips: If you have trouble remembering the directory, we can use “tab” or the file.choose() function to select the working directory.

Data input

read.table

it is usually used with .txt file, but can also used with csv file.

args(read.table)
## function (file, header = FALSE, sep = "", quote = "\"'", dec = ".", 
##     numerals = c("allow.loss", "warn.loss", "no.loss"), row.names, 
##     col.names, as.is = !stringsAsFactors, na.strings = "NA", 
##     colClasses = NA, nrows = -1, skip = 0, check.names = TRUE, 
##     fill = !blank.lines.skip, strip.white = FALSE, blank.lines.skip = TRUE, 
##     comment.char = "#", allowEscapes = FALSE, flush = FALSE, 
##     stringsAsFactors = default.stringsAsFactors(), fileEncoding = "", 
##     encoding = "unknown", text, skipNul = FALSE) 
## NULL
test1<- read.table("test.csv" , sep = ",")
test1
##   V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11 V12
## 1  1  3  5  7  9 11 13 15 17  19  21  23

read.csv

args(read.csv)
## function (file, header = TRUE, sep = ",", quote = "\"", dec = ".", 
##     fill = TRUE, comment.char = "", ...) 
## NULL
test2<- read.csv("test.csv" , header = FALSE)
test2
##   V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11 V12
## 1  1  3  5  7  9 11 13 15 17  19  21  23

readxl::read_excel()

  • excel_sheets() show all of the sheet names
  • read_excel() import xls/xlsx documents, it is the combination of read_xls() & read_xlsx()
library(readxl)
excel_sheets("test.xlsx")
## [1] "Sheet1" "Sheet2"
args(read_excel)
## function (path, sheet = NULL, range = NULL, col_names = TRUE, 
##     col_types = NULL, na = "", trim_ws = TRUE, skip = 0, n_max = Inf, 
##     guess_max = min(1000, n_max)) 
## NULL
test3<- read_excel("test.xlsx", sheet = 2 , col_names = FALSE , col_types = "text")
##col_types: "skip", "guess", "logical", "numeric", "date", "text" or "list".
test3
## # A tibble: 1 x 10
##   X__1  X__2  X__3  X__4  X__5  X__6  X__7  X__8  X__9  X__10
##   <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr>
## 1 2     4     6     8     10    12    14    16    18    20

Output data

in this section, we use the dataset women

internal dataset

library(DT)
##data() is used to show the available datasets in the environment.  
data()
##here we load dataset "women" into the enviroment.  
data(women)
datatable(women)

write.csv

write.csv(women, file = "mywomen.csv")
file.exists("D:/Rworkshop/mywomen.csv")
## [1] TRUE

write.table

write.csv(women, file = "mywomen.txt" , append = FALSE)
## Warning in write.csv(women, file = "mywomen.txt", append = FALSE): attempt
## to set 'append' ignored
file.exists("D:/Rworkshop/mywomen.txt")
## [1] TRUE