as.logical
1 | myvector[as.logical (myvector<17)] #subset data <17 |
as.character
-convert all the things to character
1 | myvector[as.logical ((myvector%%3==0) | (!(myvector%%2==0)))] |
table
-gives you list of all elements and counts them
sample
-gives you random numbers from a vector
1 | set.seed(17) ##使用这个可以随机输出固定的数据 |
rnorm
-gives you 10 random numbers from normal distribution with mean=0 and sd=1
1 | rnorm(10, mean = 0, sd = 1) |
举个栗子:估计Pi值
1 | set.seed(555) #所有人都能得到相同的结论 |
function
1 | f <- function (a,b){ |
you can plot the chart directly
1 | f <- function (x){ |
sapply
1 | x <- list(A=c(1,2,3,4,5), |
unique
unique (iris[,5])
#不重复的取出第五列所有的行
data frames
1 | iris[10, "Petal.Length"] #第一个参数是row,第二个是列 |
以物种分开序列,然后输出每个物种的平均长度
1 | s <- split(my.iris, my.iris$Species) |
生成一个列表
1 | ddf <- data.frame( |
加头
colnames(ddf2) <- c("a","b", "c","d","e")
ggplot
facet_wrap #可以按照species把图片分开
ggplot(iris,aes(Sepal.Width,Petal.Length))+geom_point(aes(color=Species))+geom_smooth(method="lm")+facet_wrap(~Species)
facet_grid #可以分开横、纵两个坐标
1 | ggplot(iris,aes(Sepal.Width,Petal.Length))+geom_point(aes(color=Species))+geom_smooth(method="lm") + facet_grid(Species~factor(iris$Sepal.Length>mean(iris$Sepal.Width))) |
theme_dark() #改变主题
ggthemes
ggsave
ggsave(file="mygraph.pdf", width=10, height=15)
or
1 | pdf("mygraph.pdf", width=10,height=15) |
plotly
https://images.plot.ly/plotly-documentation/images/plotly_js_cheat_sheet.pdf
1 | library(plotly) |