Strings and Dates
基本所有的class可以处理date (e.g., March 15, 2010),
但是不能所有都处理datetime (11:45 AM on March 1, 2010).
1 Date
可以代表日期,不能是clock时间
2 POSIXct
是一个 datetime class,可以表示到秒。 可以转换,格式,基础计算。
可以存储数据
3 POSIXlt
保存 为 9个元素的list, 包含 year, month, day, hour, minute, and second
可以用于中间过程,不用于数据存储
4 可以转换 as.Date, as.POSIXct, and as.POSIXlt
5 R中的一些库
chorn
可以用于计量经济学,和时间序列分析。不能用于时区,复杂的格式
lubridate
可以用于时区
mondate
可用于精算,月份的表示
timeDate
功能非常齐全,非常好
6 当选择时间的类的时候,尽量选择满足需求的,比较简单的类。。。。
获取字符串的长度
1 想知道string的长度
使用 nchar函数,而不是 length。length返回 vector的长度
> s <- c("Moe", "Larry", "Curly")
> nchar(s)
[1] 3 5 5
连接字符串
1 给字符串添加字符串 使用paste函数
可以设置sep
> paste("Everybody", "loves", "stats.", sep="-")
[1] "Everybody-loves-stats."
> paste("Everybody", "loves", "stats.", sep="")
[1] "Everybodylovesstats."
对vector,paste会分别连接
> stooges <- c("Moe", "Larry", "Curly")
> paste(stooges, "loves", "stats.")
[1] "Moe loves stats." "Larry loves stats." "Curly loves stats."
将上述 合成一个大的字符串, 使用collapse函数,去分隔开结果
> paste(stooges, "loves", "stats", collapse=", and ")
[1] "Moe loves stats, and Larry loves stats, and Curly loves stats"
提取子字符串
1 使用 substr(string, start,end)
> substr("Statistics", 7, 10) # Extract last 4 characters
[1] "tics"
> ss <- c("Moe", "Larry", "Curly")
> substr(ss, 1, 3) # Extract first 3 characters of each string
[1] "Moe" "Lar" "Cur"
根据分隔符分开字符串
使用strsplit()函数
> strsplit(string, delimiter)
返回一个list
对一个vector使用的话,与前面一样,对vector中的每一个元素使用strsplit( )
替代字符串
> sub(old, new, string) 替换第一个
> gsub(old, new, string) 全部替换
new old 可以使用正则表达式,如不使用,设置 fixed=TRUE
查找特殊的字符串
有特殊的字符串,注意,注意,可能输出有问题,会隐藏
Generating All Pairwise Combinations of Strings
去组合字符串的所有的元素
> m <- outer(strings1, strings2, paste, sep="")
> locations <- c("NY", "LA", "CHI", "HOU")
> treatments <- c("T1", "T2", "T3")
> outer(locations, treatments, paste, sep="-")
[,1] [,2] [,3]
[1,] "NY-T1" "NY-T2" "NY-T3"
[2,] "LA-T1" "LA-T2" "LA-T3"
[3,] "CHI-T1" "CHI-T2" "CHI-T3"
[4,] "HOU-T1" "HOU-T2" "HOU-T3"
结果是一个matrix
也可以使用as.vector 变成vector
不懂:
> m <- outer(treatments, treatments, paste, sep="-")
> m[!lower.tri(m)]
[1] "T1-T1" "T1-T2" "T2-T2" "T1-T3" "T2-T3" "T3-T3"
date
1 获取当前日期
> Sys.Date()
[1] "2010-02-11"
返回date格式
2 把一个字符串,转换为Date
as.Date()
格式为yyyy-mm-dd.
> as.Date("12/31/2010")
Error in charToDate(x) :
> as.Date("12/31/2010", format="%m/%d/%Y")
[1] "2010-12-31"
如果是其他格式,要求提供格式format
3 将date转换为字符串
as.character 或者使用format
> format(Sys.Date(), format="%m/%d/%Y")
[1] "04/01/2010"
4 使用 ?strftime 查看详细的 信息
%b
Abbreviated 简写 month name (“Jan”)
%B
Full month name (“January”)
%d
Day as a two-digit number
%m
Month as a two-digit number
%y
Year without century (00–99)
%Y
Year with century
Abbreviated 简写 month name (“Jan”)
%B
Full month name (“January”)
%d
Day as a two-digit number
%m
Month as a two-digit number
%y
Year without century (00–99)
%Y
Year with century
5 将年月日 连接转换为 Date
使用 ISOdate 函数
> ISOdate(year, month, day)
结果为POSIXct ,然后可以转换为date
> as.Date(ISOdate(year, month, day))
ISOdate 函数,也可以对vector使用,即每个vector元素都分别依次使用ISOdate 函数
6 对于year, month, day, hour, minute, and second ,
可以使用 ISOdatetime
> ISOdatetime(year, month, day, hour, minute, second)
Getting the Julian Date
提取日期的部分
1 as.POSIXlt
对于date, as.POSIXlt 将其转换为list,包含
sec Seconds (0–61)
min Minutes (0–59)
min Minutes (0–59)
hour Hours (0–23)
mday Day of the month (1–31)
mon Month (0–11) year
Years since 1900
wday Day of the week (0–6, 0 = Sunday)
yday Day of the year (0–365)
isdst Daylight savings time flag
创建一系列的日期
使用 seq 函数
> s <- as.Date("2012-01-01")
> e <- as.Date("2012-02-01")
> seq(from=s, to=e, by=1)
[1] "2012-01-01" "2012-01-02" "2012-01-03" "2012-01-04" "2012-01-05" "2012-01-06"
[7] "2012-01-07" "2012-01-08" "2012-01-09" "2012-01-10" "2012-01-11" "2012-01-12"
[13] "2012-01-13" "2012-01-14" "2012-01-15" "2012-01-16" "2012-01-17" "2012-01-18"
[19] "2012-01-19" "2012-01-20" "2012-01-21" "2012-01-22" "2012-01-23" "2012-01-24"
[25] "2012-01-25" "2012-01-26" "2012-01-27" "2012-01-28" "2012-01-29" "2012-01-30"
[31] "2012-01-31" "2012-02-01"
by 也可以有不同的含义
> seq(from=s, by="month", length.out=12)
> seq(from=s, by="3 months", length.out=4)
> seq(from=s, by="year", length.out=10)
当 by=“month” 要注意,靠近月底可能会有所不一样
> seq(as.Date("2010-01-29"), by="month", len=3)
[1] "2010-01-29" "2010-03-01" "2010-03-29"