-
Notifications
You must be signed in to change notification settings - Fork 0
/
R.html
183 lines (145 loc) · 4.78 KB
/
R.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>PEvoGEn R Essentials Cheat Sheet</title>
<style>
body { font-family: Arial, sans-serif; }
h1 { color: #333; }
.section { margin-bottom: 20px; }
.section h2 { color: #444; }
.section pre { background-color: #f8f8f8; padding: 10px; }
</style>
</head>
<body>
<h1>PEvoGEn R Essentials Cheat Sheet</h1>
<div class="section">
<h2>Directory Management</h2>
<pre>
getwd(): Get the current working directory.
# Example: current_dir <- getwd()
setwd(dir): Set the working directory to dir.
# Example: setwd("C:/Data/R")
</pre>
</div>
<div class="section">
<h2>Package Management</h2>
<pre>
installed.packages(): List installed packages.
# Example: packages <- installed.packages()
install.packages("package_name"): Install a new package.
# Example: install.packages("dplyr")
library(package_name): Load an installed package into the session.
# Example: library(dplyr)
</pre>
</div>
<div class="section">
<h2>Help and Documentation</h2>
<pre>
?function_name: Access the help page for function_name.
# Example: ?mean
help(function_name): An alternative way to access the help page.
# Example: help(mean)
args(function_name): View the arguments of a function.
# Example: args(mean)
lsf.str("package:package_name"): List all functions in a package.
# Example: lsf.str("package:stats")
</pre>
</div>
<div class="section">
<h2>Data Manipulation</h2>
<pre>
data$column_name <- NULL: Delete column column_name from data.
# Example: data$unnecessary_column <- NULL
data <- data[-c(row_index), ]: Delete rows by index from data.
# Example: data <- data[-c(1,2), ]
colnames(data) <- c("new_name1", "new_name2", ...): Change column names of data.
# Example: colnames(data) <- c("ID", "Age", "Gender")
</pre>
</div>
<div class="section">
<h2>Data Import/Export</h2>
<pre>
read.csv("file.csv"): Read a CSV file into a data frame.
# Example: my_data <- read.csv("data.csv")
write.csv(data, "file.csv"): Write a data frame to a CSV file.
# Example: write.csv(my_data, "new_data.csv")
read.table("file.txt"): Read a general flat file into a data frame.
# Example: my_table <- read.table("table.txt")
write.table(data, "file.txt"): Write a data frame to a general flat file.
# Example: write.table(my_table, "new_table.txt")
</pre>
</div>
<div class="section">
<h2>Data Exploration</h2>
<pre>
str(data): Display the structure of the data frame data.
# Example: str(my_data)
summary(data): Provide a summary of the data frame data.
# Example: summary(my_data)
head(data): Show the first few rows of the data frame data.
# Example: head(my_data)
tail(data): Show the last few rows of the data frame data.
# Example: tail(my_data)
</pre>
</div>
<div class="section">
<h2>Data Transformation</h2>
<pre>
transform(data, new_column = expression): Add a new column to data with values calculated from expression.
# Example: data <- transform(data, log_income = log(Income))
subset(data, condition): Extract subsets of rows from data based on condition.
# Example: high_income <- subset(data, Income > 50000)
merge(data1, data2): Merge two data frames by common columns or row names.
# Example: merged_data <- merge(data1, data2)
</pre>
</div>
<div class="section">
<h2>Statistical Functions</h2>
<pre>
mean(x): Calculate the mean of x.
# Example: average_age <- mean(data$Age)
median(x): Calculate the median of x.
# Example: median_income <- median(data$Income)
sd(x): Calculate the standard deviation of x.
# Example: sd_age <- sd(data$Age)
cor(x, y): Calculate the correlation between x and y.
# Example: correlation <- cor(data$Age, data$Income)
</pre>
</div>
<div class="section">
<h2>Plotting</h2>
<pre>
plot(x, y): Create a scatter plot with x and y.
# Example: plot(data$Age, data$Income)
hist(x): Create a histogram of x.
# Example: hist(data$Age)
boxplot(x): Create a boxplot of x.
# Example: boxplot(data$Income)
barplot(height): Create a bar plot with heights defined by height.
# Example: barplot(height = c(10, 20, 30))
</pre>
</div>
<div class="section">
<h2>Control Structures</h2>
<pre>
if (condition) { ... }: Execute code if condition is true.
# Example: if (mean(data$Age) > 30) { print("Average age is greater than 30") }
for (variable in sequence) { ... }: Execute a loop over a sequence.
# Example: for (i in 1:10) { print(i) }
while (condition) { ... }: Execute code while condition is true.
# Example: count <- 0; while (count < 10) { print(count); count <- count + 1 }
repeat { ... }: Execute an infinite loop (use break to exit).
# Example: repeat { print("This will print once"); break }
</pre>
</div>
<div class="section">
<h2>Function Definition</h2>
<pre>
function(arg1, arg2, ...) { ... }: Define a new function with arguments.
# Example: add <- function(x, y) { return(x + y) }
</pre>
</div>
</body>
</html>