Data Structure
Vector
c(9,4,3)
vector("logical", 10)
vector("complex", 10)
vector("integer", 10)
seq(1, 5, 2) #seq(from, to, by, length.out) length.out表示均匀地取出若干个数
seq(1,5,,9)
rep(1:20,, 10, ) #rep(..., times, length.out, each)
rep(1:2, 10, 9, )
x = 100
is.complex(x) #查数据类型
as.complex(x) #数据类型转换
as.character(90.9L)
as.integer(-5.5)
as.integer(5.8)
# round up to 0
(1:8)*(1:3) #逐次乘,按元素操作,短的会被循环
(1:8)^5 #vector-scalar : a special case of the vector-vector operation
Factor
mycolor<-factor(c("R","G","G","R","B"),levels =c("R","G","B"),labels=c("RED","GREEN","BLUE"))
Matrix
# matrix(data, nrow, ncol, byrow, dimnames = list(rownames, colnames))
m = matrix(c(1,2,3,3,0,9,0,8,4), 3, 3)
solve(m) # inverse
diag(m) # diagonal
t(m) # transpose
A = matrix(1:4,2,2)
b=c(1,2)
A%*%b #矩阵乘法
A*b #数乘
b*A
t(b)%*%A
t(b)*A
Read & Save
csv
df = read.csv("./data/example-utf8.txt", fileEncoding = "UTF-8", stringsAsFactors = FALSE, dec = '.') #dec:decimal
head(df)
summary(df)
txt
df = read.delim("./data/RA_concepts.txt", sep = "|", header = TRUE)
head(df)
summary(df)
Excel
library("xlsx")
x = xlsx::read.xlsx2("./data/÷–“Ω≤°¿˙.xlsx", sheetIndex=1)
library(readxl)
# Read all Excel sheets with lapply(): pop_list
my_workbook <- lapply(excel_sheets("./data/÷–“Ω≤°¿˙.xlsx"), read_excel, path = "./data/÷–“Ω≤°¿˙.xlsx")
# Display the structure of pop_list
str(my_workbook)
Search
df[which.max(df$Code),] #注意后面有逗号
which.min(df$Code)
Edit
# Edit the colClasses argument to import the data correctly: hotdogs2
hotdogs2 <- read.delim("./data/RA_concepts.txt", header = FALSE,
col.names = c("type", "calories", "sodium"),
colClasses = c("factor", "NULL", "numeric")) #注意NULL也需要打引号
# Display structure of hotdogs2
str(df)
Write
write.table(df, file = "./data/test.csv", quote = FALSE, sep = '\t', col.names = c("A","B","C"))
sink("output.txt") #在命令行中打,就是把print的结果都输出到output.txt中
print("Hello")
sink() #重新在terminal中输出
Encoding
junk = read.delim("./data/RA_concepts.txt", nrows = 10, sep = "|")
classes <- sapply(junk, class)
classes
data <- read.delim("./data/RA_concepts.txt", colClasses = classes, sep = '|')
head(data)
library("readr") # readr: automatically get the encoding of a file
dat = readr::read_delim("./data/RA_concepts.txt", delim=',')
readr::guess_encoding("./data/RA_concepts.txt")
Function
Basic Function
my.plot = function(dev, file, w, h, ...){
do.call(dev, list(file, w, h))
plot(...)
dev.off()
}
my.plot("pdf", "junk.pdf", 5, 3, sin, 0, 2*pi, main = "The Sine Function")
# main是标题
... Argument
powered.sum = function(..., power = 2){
a = c(...)
return (sum(a^power))
}
powered.sum(1, 3, 5:7, power = 3) #需要在输入中specify power
Plot Function
score = c(1,2,3,1,2,5,1,2,78)
fn = ecdf(score) #empirical cdf
plot(fn, main = "Empirical CDF")
Iteration
a = runif(10) #rodom numbers
yes = c("A", "B", "C")
no = c("a", "b", "c")
a
ifelse(a>0.5, yes, no)