R


R 语言语法

  1. 查看当前目录 getwd()
  2. 切换指定目录 setwd()

1. Subsetting data选取数据

student[student$gender=='Female']
attach(student)
grade[Chinese>80,]
grade[Chinese>80 & Math>80,]
grade[Chinese>80 | Math>80,]
detach]

2. Sorting data

# sort by Chinese asc
order(grade$Chinese) # 排出升序的行号
oder(-grade$Chinese) # 进行降序的行号
grade[order(grade$Chinese),] #升序排序的dataFrame
# or use attach
attach(grade)

# sort by Chinese asc
grade[order(Chinese),]
# sort by Chinese asc, English des
grade[order(Chinese,-English),]
detach(grade)]

3. Merging data

# merging data by giving column names
total <- merge(student,grade,by="name")
head(total)

4. Aggregating Data

  • aggregate(formula,data,FUN)
# compute the average Math grade by class
aggregate(Math~class, data=total,FUN=mean)

# compute the average grade by class and gender
aggregate(cbind(Chinese,Math,English,Science)~class+gender,data=total,FUN=mean)

Tips

paste(a,b,c) # 将所有内容组合出来,以sep=“ ”为分割

5. 自定义函数

Define a function:
function_name <- function (){
    ## Do something interesting 
    return(result)
} 

文章作者: Shen Hao
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 Shen Hao !
评论
评论
 上一篇
Dockerfile Dockerfile
dockerfile的配置命令以及运行命令
2020-09-23
下一篇 
How to use docker How to use docker
前提详述:安装Docker 环境准备 Linux要求内核3.0以上 安装 ➜ ~ uname -r 4.15.0-96-generic # 要求3.0以上 ➜ ~ cat /etc/os-release NAME="
2020-09-20
  目录