R語言筆記(二

2021-10-03 07:37:46 字數 1909 閱讀 6701

因子(factor)

元素型別可以不同

> df <-data.frame(id=c(1,2,3,4),name=c("a","b","c","d"))

> df

id name

1 1 a

2 2 b

3 3 c

4 4 d

> nrow(df)

[1] 4

> ncol(df)

[1] 2

> data.matrix(df) #轉換為矩陣

id name

[1,] 1 1

[2,] 2 2

[3,] 3 3

[4,] 4 4

日期與時間

日期:date

> dt<-date()

> dt

[1] "fri feb 28 11:25:28 2020"

> class(dt)

[1] "character"

> dt <-sys.date()

> dt

[1] "2020-02-28"

> class(dt)

[1] "date"

> x3 <- as.date("2015-01-01") #儲存日期

> x3

[1] "2015-01-01"

> weekdays(x3)

[1] "thursday"

> months(x3)

[1] "january"

> quarters(x3)

[1] "q1"

> julian(x3)

[1] 16436

attr(,"origin")

[1] "1970-01-01"

x3 <- as.date("2015-01-01")

x4 <- as.date("2016-01-01")

x4-x3

time difference of 365 days

> as.numeric(x4-x3) #強制轉換

[1] 365

距離1970-01-01的秒數/sys.time()

posixct:整數,常用語存入資料框

posixlt:列表,還包含星期,年,月,日等資訊

> x5 <- sys.time()

> x5

[1] "2020-02-28 11:34:31 cst"

> class(x5)

[1] "posixct" "posixt"

> p<-as.posixlt(x5)

> p

[1] "2020-02-28 11:34:31 cst"

> class(p)

[1] "posixlt" "posixt"

> names(unclass(p)) #獲取屬性名稱

[1] "sec" "min" "hour" "mday" "mon" "year" "wday"

[8] "yday" "isdst" "zone" "gmtoff"

> p$sec #檢視變數的值

[1] 31.83603

> p$yday

[1] 58

> as.posixct(p)

[1] "2020-02-28 11:34:31 cst"

> x6 <- "jan1,2015 01:01"

[1] "jan1,2015 01:01"

> strptime(x6,"%b %d, %y %h:%m")

[1] "2015-01-01 01:01:00 cst"

R語言學習筆記(二)

ps 內容 於網路,僅供學習參考,請勿用於商業用途 主要是補充部分 驗證 無論隨機變數原來服從哪種分布,只要樣本容量足夠大,其均數都會服從正態分佈 1.正態分佈 a rnorm 10000,0,1 生成乙個資料量很大的正態分佈的資料 x 1 100 生成乙個向量用來存放樣本均數的向量 a data....

R語言筆記

賦值 x 賦多個值 x統計函式 樣本標準差 sd arg 平均數 mean arg 兩個資料間相關性 變動趨勢相關性 cor a,b 線性模型 lm formula,data data linear models 用來做線性擬合的函式 biglm 能以記憶體高效的方式實現大型資料的線性模型擬合 處理...

R語言筆記

r語言正則內容 str grep function pattern,x,ignore.case false,perl false,value false,fixed false,usebytes false invert false pattern 模式 ignore.case 是否忽略大小寫 va...