R學習筆記02 2021 01 29

2021-10-18 07:37:13 字數 4724 閱讀 5425

2021-1-26整理

#----------------------資料讀取--------

read.table(

'name.txt'

,header = f/t,sep =

',',nrows=

, col.names =

)#讀取txt檔案,header:是否把首行當作表頭,sep:分隔符

#nrows:最大讀取行數,col.names:列名(header=t時col.name將其替換)

#incomplete final line found by readtableheader on 'data-1.txt'

#try to加一行空格when u see such a waring.

#error in make.names(col.names, unique = true) : 多位元組字串2有錯

#把中文表頭改為英文

matrix(data=

, nrow=

, ncol=

, byrow = f)

#no need to tap 'nrow'&'ncol'

summary(

)#總結

str(

)#the same as above

dim(

)#檢視維度,資料有幾行幾列

colsums(

)#列求和

#--------額外的知識--------

dat <- c(

'1',

'2',

'3',

'4')

dim(dat)

<- c(2,

2)2,as.numeric)

#檔名。1-行、2-列。as.numberic:轉化為數值型

dat1 <- c(1,

2,3,

4)dat2 <- c(2,

3,4,

5)dim(dat1)

<- c(2,

2)dim(dat2)

<- c(2,

2)rbind(dat1,dat2)

#在data1後邊加上data2

cbind(dat1,dat2)

#same as above

for(i in

1:n)

cat(

)#catenate:連線字串,可以直接顯示連線後的函式

#-----

#----------12/23----------

seq(from=

1,to=

10,by=2)

seq(from=

0,to=

10,length.out =5)

rep(1,

5)x <- c(1,

2,3)

rep(x,each=2)

rep(x,each=

2,time,times=2)

#-----

#-----12/28-----

length(

)#向量長度

x <- c(1,

2,3,

4,5)

x[-1]

#不輸出第幾個元素

x[c(1,

4,2,

1)]x[c(t,f,f,t)

]#輸出邏輯值為真的數值,多餘的邏輯值會輸出na

x[c(t,t,t,f,f,t)

]# %in% 判斷字元是否在這個向量中

z <- c(

"one"

,"two"

,"three"

)"one"

%in%z

k <- z %in% c(

"one"

,"two"

,"three"

,"four"

)z[k]

#命名

y <- c(1,

2,3,

4)names(y)

<- c(

"a",

"b",

"c",

"d")

y["a"

]

#新增向量

x[10

]<-1 x

x[c(4,

5,6)

]<- c(12,

23,45)

x99,after =2)

#在第2個元素後邊新增

xx1

#修改向量x[2

]<-98x

#刪除向量

rm(a)

##刪除整個向量

y1 <- y[

-c(1:3

)]y1t

#-----

#-----1/2-----

#for the second day of new year

a <- mtcars

name_abbr <- substr(names(a),1

,2)#substr(x= , start= , stop= )

names(a)

<- toupper(name_abbr)

a <-

''#-----

#-----

x <- c(

342,61)

chisq.test(x)

#------字串-----

#匹配單個列出的字元[···]

#排除性字元,匹配單個未列出的字元 [^···]

#匹配任意字母、數字、下劃線 \w

#匹配任意空白字元\s

#匹配任意數字\d:前面數字出現連續兩次,

#匹配單詞的開始或結束 \b

#轉義字元 \char

#一些函式

nchar('hello world')#count the number of this character,including the space

length(c('hello','world','**** u'))#count number of elements

a <- c('hello')

b <- c(1,2,3)

c <- c('world')

m <- paste(a,c)

paste('everybody','will','love','u',sep = '-')

length(m)

nchar(m)

paste(b,'is cool')

b1 <- c('a','b','c')

paste(b1,'is cool')

m.n <- month.name

m.a <- substr(m.n,start=1,stop=3)#提取字串,從第1個到底3個

toupper(m.a)

tolower(m.a)

sub()

gsub()

grep()

x <- c('b','a+','ac')

grep('a+',x,fixed = t)#return 所要搜尋的字串的下標

grep('a+',x,fixed = f)

match("ac",x)#return 所要搜尋的字串的位置

b %in% x#向量b知否在x中(b <- c(1,2,)),依次匹配b中元素

"b"%in%x#字元b是否在x中

path <- '/users/local/bin/r'

strsplit(path,'/')#所得到的結果是list

face <-c('spandes','clubs','hearts','diamonds')

suit <- (1:13)

outer(suit,face,fun = paste,sep='/')

#-----日期資料--1/25----

#1.描述已有日期內發生了什麼

#2.**未來將會發生什麼

sunspots#太陽黑子數目

presidents#美國**支援率

class(presidents)

#ts代表時間序列

typeof(presidents)

airquality#空氣質素

class(airquality)

#data.frame

airmiles

class(airmiles)

sys.date(

)#show today

class(sys.date())

#date

a <-

'2021-01-25'

as.date(a,format =

"%y-%m-%d"

)#y is cap,m、d isnot

strftime

day <- seq(as.date(

'2017-01-01'

),as.date(

'2020-01-25'

),by=60)

sales <- round(runif(

48, min=

50, max=

100)

)ts(sales,start = c(

2010,5

),end=c(

2014,4

),frequency =1)

#1:year,4:season,12:month

R語言學習筆記 小試R環境

買了三本r語言的書,同時使用來學習r語言,粗略翻下來感覺第一本最好 r語言程式設計藝術 the art of r programming r語言初學者使用 a beginner s guide to r r語言實戰 r in action 一句話簡介r語言 r是一種用於資料處理和統計分析的指令碼語言...

R語言學習筆記 小試R環境

買了三本r語言的書,同時使用來學習r語言,粗略翻下來感覺第一本最好 r語言程式設計藝術 the art of r programming r語言初學者使用 a beginner s guide to r r語言實戰 r in action 一句話簡介r語言 r是一種用於資料處理和統計分析的指令碼語言...

R語言學習筆記

1.資料輸入 read.table n fileposition 函式 eg.x read.table c users administrator desktop 1.txt header t 目前只知道可以完好地讀入txt檔案 空格隔開 excel不支援 csv讀入不完全。ps.可以用excel生...