基本数据结构

矩阵

# 生成矩阵
matrix(vector, byrow = TRUE, nrow = 5, dimnames = list(c(), c(), ...)
# 矩阵求和
rowSums(Amatrix) 
colSums()
# 合并其他的矩阵或向量
cbind()
rbind()
# 矩阵选择元素,要用逗号分开行列,vector就不需要了。

#  矩阵行、列名
colnames(matrixA) <- c.vectorname
rownames(matrixA) <- c.vectorname

因子

# 把一组变量变成因子
factor(vector)
# 无序因子
factor1 <- factor(vector)
# 有序因子 order = TRUE
factor2 <- factor(vector, order = TRUE, levels = c("Low", "Medium", "High"))
# 结果
> [1] High   Low    High   Low    Medium
Levels: Low < Medium < High

# 修改因子中的名称
levels(factor1) <- c("", "", "")
# 无序因子强行进行比较,WARNING!
> v <- c("medium", "low", "high", "low")
> v_f <- factor(v, levels = c("low", "medium", "high"))v
> v_f[1] > v_f[3]
[1] NA
Warning message:
In Ops.factor(v_f[1], v_f[3]) : ‘>’ not meaningful for factors

注意:summary(vector) 和 summary(factor) 的差异
提取因子中的元素: factor[1]
因子中的元素是可以比较大小的

data frame

# 生成数据框
data.frame()

# 选择数据框中的元素
dataframe[]
dataframe[1:3,"type"] # 选择 “type”列,1-3个元素
dataframe[, "type"]
dataframe$type

order

把所有元素从小到大排序后,返回每个值在原来的位置。
用来 rearrange data

> a <- c(4,6,12,20,1,7,13)
> order(a)
[1] 5 1 2 6 3 7 4
> a[order(a)] # 把 a 按照从小到大排序
[1]  1  4  6  7 12 13 20

list

datacamp 的一门课中将 list 类比成为 to-do-list,你的每一条 to-do-list 的内容、格式会各种各样。

# 生成 list
list(component1, component2, ...)
# 生成带有名称的 list
my_list <- list(name1 = your_comp1, 
                name2 = your_comp2)
# 先生成 list, 再命名
my_list <- list(your_comp1, your_comp2)
names(my_list) <- c("name1", "name2")

# 选取元素
my_list[["sa1"]]
my_list$sa1
my_list[[2]]

# nested list 选取元素
x <- list(a = 1, b = list(r = 2, s = 3))
x[[c("b", "r")]]

# 增加元素
newlist <- c(oldlist, newcomponent)
newlist <- c(oldlist, anyname = newcomponent)

Just a tip before you get to it:
If you have a list of lists a and want to add a list b to it, you can use c(a, list(b)).
一定是 c(a, list(b)) 而不是c(a, b)

日期

当前日期

> today <- Sys.Date()
> today
[1] "2019-10-09"

当前时间

> now <- Sys.time()
> now
[1] "2019-10-09 20:35:21 CST"

创建日期

> adate <- as.Date("2008-8-1")
> adate
[1] "2008-08-01"
> class(adate)
[1] "Date"

> bdate <- as.Date("2008-20-8")
Error in charToDate(x) : 字符串的格式不够标准明确
> bdate <- as.Date("2008-20-8", format = "%Y-%d-%m")
> bdate
[1] "2008-08-20"

解析日期

> parse_date("17 August 2010", format = "%d %B %Y")
[1] "2010-08-17"

创建时间

> ctime <- as.POSIXct("2008-8-20 10:12:15")
> ctime
[1] "2008-08-20 10:12:15 CST"

format

%Y: 4-digit year (1982)
%y: 2-digit year (82)
%m: 2-digit month (01)
%d: 2-digit day of the month (13)
%A: weekday (Wednesday)
%a: abbreviated weekday (Wed)
%B: month (January)
%b: abbreviated month (Jan)

%H: hours as a decimal number (00-23)
%I: hours as a decimal number (01-12)
%M: minutes as a decimal number
%S: seconds as a decimal number
%T: shorthand notation for the typical format %H:%M:%S
%p: AM/PM indicator

加减运算

> adate
[1] "2008-08-01"
> adate + 10
[1] "2008-08-11"
> Sys.Date() - adate
Time difference of 4086 days
> ctime
[1] "2008-08-20 10:12:15 CST"
> ctime + 50
[1] "2008-08-20 10:13:05 CST"
> Sys.time() - ctime
Time difference of 4067.445 days

在程序中,日期、时间的起点是1970年1月1日,存储为 numeric格式的数字,因此可以进行加减运算。
使用 unclass函数可以把日期变为 numeric。

> unclass(adate)
[1] 14092
> unclass(as.Date("1970-1-1"))
[1] 0
> unclass(as.Date("1970-1-2"))
[1] 1

处理日期好用的包

library(lubridate)
library(zoo)
library(xts)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值