在这里插入
"数据基本处理"
#创建函数
myfunction <- function(){
x <- rnorm(100)
mean(x)
}
#nothing print
second <- function(x){
x + rnorm(length(x))
}
x <- 1:20
print(x)
#获取当前的空间
getwd()
#设置工作空间
setwd("D:")
#加载其它的包
source("mycode.R")
#显示当前有哪些函数
ls()
"数据类型:矢量"
g=c(1,"a")
g
'数据类型 matric '
## 数据类型 矩阵
#way1
m<- matrix(nrow=2,ncol=3)
m
#way2
m<- matrix(1:6,nrow=2,ncol=3)
m
#[,1] [,2] [,3]
#[1,] 1 3 5
#[2,] 2 4 6
#way3
m<- 1:10
m
dim(m)<-c(2,5)
m
#[,1] [,2] [,3] [,4] [,5]
#[1,] 1 3 5 7 9
#[2,] 2 4 6 8 10
#先列再行
#way4 cbind rbind
x<-1:3
y<-10:12
cbind(x,y)
rbind(x,y)
'数据类型 factors '
#function:记录分类的数据,可以记录有序的数据也可以记录无序的数据
x<- factor(c('yes','yes','no','yes','no'))
x
# 分类标签和数量
table(x)
#把分类用数字形象的表示出来,把数值类型变成整型
unclass(x)
#表示出label 表示单独的属性。这里默认为no为基线水平,而yes是第二个水平
attr(x,"levels")
#把yes设置为基线水平
x<- factor(c('yes','yes','no','yes','no'),
levels=c('yes','no'))
x
'Missing Values的数据类型'
#NaN表示未定义的数学运算,NA表示其他缺失值
x<- c(1,2,NA,10,3)
is.na(x)
is.nan(x)
x<-c(1,2,NaN,NA,4)
is.na(x)
is.nan(x)
'Data Frames的数据类型'
# 是用来储存数据表格的重要数据类型
# 数据框内可以是不同的类型
# 可以通过read.table*()和read.csv()来创建数据框,也可以通过data.frame()来创建。
x<-data.frame(foo= 1:4,bar= c(T,F,T,F))
x
nrow(x)#输出类别
ncol(x)
'names的数据类型'
x<-1:3
names(x)
x
#null
names(x)<-c("foo","bar","norf")
x
names(x)
# 列表也是有名字的
x<-list(a=1,b=2,c=3)
x #会对应输出相应的名字
# metrix也有名字
m<-matrix(1:4,nrow = 2,ncol = 2)
dimnames(m)<-list(c('a','b'),c('c','d'))
m
"R读写数据"
read.table()是最常用的
#read.table()默认分割符为空格,而read.csv()默认分割符为逗号
# 其他就没有什么差别了
# csv comma separated value
"用R读取大量的数据集"
#如果没有注释的话
可以设置comment.char = ""
# 设置数据类型(加快速度),设置行(能够节约内存)
colClasses="numeric"
nrow =100
#了解自己电脑的情况
# 1、计算机有多大内存?
# 2、计算机有多大物力内存?
"保存的数据类型"
# dput
y<- data.frame(a =1, b="a")
dput(y)
structure(list(a=1,
b=structure(1L, .Label= "a",
class = "factor")),
.Names =c("a","b"),row.names=c(NA,-1L),
class = "data.frame")
dput(y,file="y.R")
new.y<- dget("y.R")
new.y
#dump
x<-"foo"
y<- data.frame(a=1 ,b="a")
dump(c("x","y"),file="data.R")
rm(x,y)
source("data.R")
y
"与外界世界连接"
# connection
con<- file("foo.txt","r")
data<-read.csv(con)
close(con)
# is the same as
data<- read.csv("foo.txt")
#Reading Lines of a Text File
con<- gzfile("word.gz")
x<- readLines(con,10)
x
# html
con<- url("http:www.google.com","r")
x <-readLines(con)
head(x)
"基础数据类型"
#单括号只能一个数据类型,双括号可以多个数据类型
#可以创建一个逻辑符号
#数字索引
x<- c("a","b","c","d","e")
u<-x>"a"
u
#逻辑索引
x[u]
"Subsetting List"
x<- list(foo=1:4, bar= 0.6)
x[1]
#$foo
#[1] 1 2 3 4
x[[1]]
#[1] 1 2 3 4
#返回名字为"bar"的元素
x$bar
#[1] 0.6
#获取一个列表的多个元素,获取第1个和第3个元素
x<- list(foo=1:4, bar= 0.6, baz="hello")
x[c(1,3)]
#注意
x<- list(foo=1:4, bar= 0.6, baz="hello")
name<-"foo"
x[[name]]
#[1] 1 2 3 4
#对一个变量进行赋值,不能用$name
x$name
#NULL
x$foo
#[1] 1 2 3 4
x<- list(a=list(10,12,14),b=c(3.14,2.18))
x[[c(1,3)]]
#[1] 14
x[[1]][[3]]
# [1] 14
x[[c(2,1)]]
#[1] 3.14
"对矩阵子集化"
x<-matrix(1:6, 2, 3)
x[1, 2]
x[2,1]
x[1,]
x[,2]
#返回的是元素,而不是矩阵
#使用单层括号时,返回的对象和原对象类型相同
x<-matrix(1:6, 2, 3)
x[1, 2]
x[1, 2, drop = FALSE][,1]
#当drop为true时,对应的是个矩阵
x<-matrix(1:6, 2, 3)
x[1, ]
x[1, ,drop = FALSE]
#当不是自己想要的类型时,直接把drop = FALSE
#这样即为相反的值
"模糊匹配"
x<- list(aardvark = 1:5) # 数列
x$a
x[["a"]] # 准确搜索
x[["a", exact = FALSE]]
"去除缺失值"
x<- c(1 ,2 ,NA, 4, NA, 5)
bad <- is.na(x)
x[!bad]
#[1] 1 2 4 5
#Removing NA Value
x<- c(1, 2, NA, 4, NA,5)
y<- c("a","b",NA ,"d", NA, "f")
good <- complete.cases(x, y)
good
#[1] TRUE TRUE FALSE TRUE FALSE TRUE
x[good]
#[1] 1 2 4 5
y[good]
#[1] "a" "b" "d" "f"
"向量"
x<- 1:4; y<-6:9
x+y
#[1] 7 9 11 13
x>2
#[1] FALSE FALSE TRUE TRUE
y==8
#[1] FALSE FALSE TRUE FALSE
x*y
#[1] 6 14 24 36
x/y
#[1] 0.1666667 0.2857143 0.3750000 0.4444444
x<- matrix(1:4, 2, 2);y<- matrix(rep(10,4), 2, 2)
x*y
#[,1] [,2]
#[1,] 10 30
#[2,] 20 40
x/y
#[,1] [,2]
#[1,] 0.1 0.3
#[2,] 0.2 0.4
x %*% y #matrix calculate
#[,1] [,2]
#[1,] 40 40
#[2,] 60 60代码片
R学习——霍普金斯大学week1
最新推荐文章于 2025-01-16 14:57:06 发布
216

被折叠的 条评论
为什么被折叠?



