-
Notifications
You must be signed in to change notification settings - Fork 0
/
R.Training.2.2.R
219 lines (173 loc) · 6.55 KB
/
R.Training.2.2.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
###########################
## 2. Advanced Operation ##
## 2.2 Input and Output ##
###########################
#---------------#
# Read R Script #
#---------------#
# source()
# sourcing from local directory
source("r.ex.R")
# sourcing from Internet
source("https://raw.githubusercontent.com/elliott828/boulot-test/master/r.ex.R")
# OPTIONAL: OS X system cannot read content from "https://"
if(!"RCurl" %in% installed.packages()){install.packages("RCurl")} # CHECK whether "RCurl" is installed
library(RCurl)
ex <- getURL("https://raw.githubusercontent.com/elliott828/boulot-test/master/r.ex.R",
ssl.verifypeer=0L, followlocation=1L)
writeLines(ex, "temp.R")
source("temp.R")
#=====================================================================
#-----------------------------#
# Export Data / Write A Table #
#-----------------------------#
# write.table()
?write.table
?mtcars
write.table(mtcars, file = "mtcars1.txt")
write.table(mtcars, file = "mtcars2.txt", sep = ",", col.names = F, row.names = F)
# export table to specified directory
write.table(mtcars, file = "C:/Users/mtcars3.txt")
write.table(mtcars, file = "C:\\Users\\mtcars4.txt")
# be careful on the direction of slash
# The address in WINDOWS platform is backslash: "C:\Users\R-Test"
# R read "C:\Users\R-Test" as "C:UsersR-Test" -- "\" is the escape symbol
# Using slash or double backslash for specifying the address in write.table()
# That also applies while using other message output functions (will be mentioned below)
#---------------------------------------------------------------------
# what if i want to save the data frame to a .csv file?
write.table(mtcars, file = "mtcars1.csv", sep = ",")
# the column names are not correctly matched with the data.
# or, we can try this function:
write.csv(mtcars, "mtcars2.csv")
# function specifies "," as separator
# and "." as decimal point
# write.csv2()?
# function specifies ";" as separator
# and "," as decimal point
# i.e. French system usually uses this format
#=====================================================================
#--------------#
# Read A Table #
#--------------#
# read a file in table format and creates a data frame from it,
# with cases corresponding to lines and variables to fields in the file.
# ?read.table
# read.table(file, header = F, sep = "", ...)
read.table("mtcars1.txt") -> table1
read.table("mtcars1.txt", header = T) -> table2
# Why header is not set as the default value: False?
# header is set to TRUE if and only if
# the first row contains ONE FEWER field than the number of columns.
read.table("mtcars1.txt", header = F) -> table3
# Error message generates
write.table(mtcars, file = "mtcars2.txt", col.names = F)
read.table("mtcars2.txt") -> table4
table4
#---------------------------------------------------------------------
# read .csv data
# read.csv(file, header = TRUE, sep = ",")
table5 <- read.csv("mtcars.csv")
# read.csv2(file, header = TRUE, sep = ";")
# corresponding to write.csv2()
#=====================================================================
#-------------------------#
# Read & Write Excel File #
#-------------------------#
# What if I want to read and write .xls / .xlsx files?
# search on the Internet
# many solutions are provided: XLConnect, gdata, xlsx
install.packages("XLConnect")
# or install.packages("xlsx")
# for these 2 packages above make sure "Java" is installed in your PC (free download)
# for "gdata", make sure "Perl" is available in your PC
library(XLConnect)
# or use require(), realize the previous steps together
require(XLConnect)
# write.xlsx(data frame, file)
write.xlsx(mtcars, file = "mtcars.xlsx")
write.xlsx(mtcars, file = "mtcars.xls")
# read.xlsx(file, worksheet name / worksheet index)
read.xlsx("mtcars.xlsx", "Sheet1")
# or
read.xlsx("mtcars.xls", 1)
#=====================================================================
#-----------------#
# Prompt Messages #
#-----------------#
# print()
print("We've learned 6 sesssions!")
x <- "We've learned 6 sesssions!"
print(x)
print(x, quote = F)
class(print("We've learned 6 sesssions!"))
#---------------------------------------------------------------------
# message()
y <- "How many sessions left?"
message(y)
class(message("How many sessions left?"))
#---------------------------------------------------------------------
# cat()
# concatenate and print
cat("My hometown is Beijing")
cat("My hometown is ", "Beijing", sep = "")
hometown <- c("Beijing", "Shanghai", "Guangzhou", "Shenzhen", "Chengdu", "Chongqing")
cat("My hometown is ", hometown[sample(1:6,1)],sep = "")
# no need to specify replace = 1 since the sample size is 1.
#---------------------------------------------------------------------
# what if I want to print more than 1 row?
cat("All things in their being are good for something.\nR in its being is good for MSU.")
# use "\n" to break the line
# or:
cat("All things in their being are good for something.",
"R in its being is good for MSU.", sep = "\n")
# warning messages
# warning()
warning("test")
budget <- 300 # budget for a booklist
book <- 27 # avg. price per book
total <- 0 # total cost
for (i in 1:20){
total <- total + book
if (total > budget){
cat("You have bought", i-1 , if(i>1){"books!"}else{"book!"}, sep =" ")
warning("Not enough money, Bookworm!")
break
}
}
#---------------------------------------------------------------------
# warnings()
# print last warning message in a "pleasing" form
warning("Oh! Don't Laugh at Me!")
warnings()
# assign("last.warning", NULL, envir = baseenv())
# warnings(...)
# concatenate with the last warning message, argument to be passed to cat()
warnings("Dude!")
no.books <- 11
warnings("Dude! You have bought", no.books, "books!", sep = " ")
#---------------------------------------------------------------------
# Session 2.2 Review
# - Read R script
# * source()
# * sourcing from local directory or from online
# * difference between WINDOWS and OS X
# - Export / write a table
# * write .txt file: write.table()
# * write .csv file: write.csv()
# - Read a table
# * read .txt file: read.table()
# * read .csv file: read.csv()
# - Read & write Excel file
# * install packages: install.packages(), library(), require()
# * the most popular EXCEL packages:
# ~ XLConnect: write.xlsx(); read.xlsx()
# ~ xlsx
# ~ gdata
# * make sure relative software (e.g. java, perl, etc.) installed
# - Prompt messages
# * print a message: print() / message()
# * concatenate and print: cat()
# * warning message: warning(), warnings()
#---------------------------------------------------------------------
# finished on Thu. 10/30/2014