全部版块 我的主页
论坛 数据科学与人工智能 数据分析与数据科学 R语言论坛
10211 15
2012-01-28
悬赏 100 个论坛币 已解决
案例文件:ggplot2包中的economics,
完整命令:
year <- function(x) as.POSIXlt(x)$year + 1900
qplot(unemploy / pop, uempmed, data = economics,geom = "path", colour = year(date)) + scale_area()

不明白的是上面红体部分。

我想看一看新编制的year函数的效果,就如下操作了:
year <- function(x) as.POSIXlt(x)$year + 1900
year(date)
但提示错误:
错误于as.POSIXlt.default(x) :
  do not know how to convert 'x' to class "POSIXlt"

最佳答案

kaifengedu 查看完整内容

简单地理解,它是时间转换的一种格式,年代从1900年算起。 > as.POSIXlt("1900-01-02")$year [1] 0 > as.POSIXlt("1901-01-02")$year [1] 1 > as.POSIXlt("1899-01-02")$year [1] -1 就是1900年抽取的数字是0,1901年抽取的是1,1899年抽取的是-1,为了表示你想要的年代,再加上1900。 所以 > as.POSIXlt("1900-01-02")$year+1900[1] 1900> as.POSIXlt("1901-01-02")$year+1900[1] 1901> as.POSIXlt("1899-01-02")$year+190 ...
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

全部回复
2012-1-28 14:13:01
简单地理解,它是时间转换的一种格式,年代从1900年算起。
> as.POSIXlt("1900-01-02")$year
[1] 0
> as.POSIXlt("1901-01-02")$year
[1] 1
> as.POSIXlt("1899-01-02")$year
[1] -1
就是1900年抽取的数字是0,1901年抽取的是1,1899年抽取的是-1,为了表示你想要的年代,再加上1900。
所以
> as.POSIXlt("1900-01-02")$year+1900[1] 1900> as.POSIXlt("1901-01-02")$year+1900[1] 1901> as.POSIXlt("1899-01-02")$year+1900[1] 1899

你的测试写成
year <- function(x) as.POSIXlt(x)$year + 1900
year(economics$date)

就可以了!

P.S. 楼主也在看ggplot2 Elegant Graphics for Data Analysis这本书?



二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2012-1-28 14:28:12
建议楼主看看R cookbook,里面的东西挺实用的
Scale_area是控制图形大小的一个方式,主要是点图吧,具体请看:http://had.co.nz/ggplot2/scale_size.html
在这个图里面实际上是没有什么作用的。

楼主看书真仔细,
这本书写的一点也不好,虽然看过了,但是很多没有掌握


The POSIXlt object represents a date as a list of date parts. Convert your Date object to
POSIXlt by using the as.POSIXlt function, which will give you a list with these members:
sec
Seconds (0–61)
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






二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2012-1-28 14:58:05
firelife 发表于 2012-1-28 14:28
建议楼主看看R cookbook,里面的东西挺实用的
谢谢楼上朋友,您给出的是一般性的理论,能否针对我的具体问题给出具体的解答?
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2012-1-28 15:05:09
mydate <-as.Date("12/31/2010", format="%m/%d/%Y")
mydate2 <-ISOdate(2011,1,29)
year <- function(x) as.POSIXlt(x)$year + 1900   
year(mydate)
year(mydate2)

x变量本身就需要是个日期格式的object
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2012-1-28 15:07:50
http://had.co.nz/
It is a gooe web for me.Many thanks!
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

点击查看更多内容…
相关推荐
栏目导航
热门文章
推荐文章

说点什么

分享

扫码加好友,拉您进群
各岗位、行业、专业交流群