楼主: 耕耘使者
9520 15

[问答] year <- function(x) as.POSIXlt(x)$year + 1900的解释 [推广有奖]

贵宾

学术权威

39%

还不是VIP/贵宾

-

威望
4
论坛币
1812817 个
通用积分
148.0401
学术水平
109 点
热心指数
173 点
信用等级
87 点
经验
93394 点
帖子
4550
精华
0
在线时间
2845 小时
注册时间
2006-4-6
最后登录
2024-2-24

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 ...
关键词:year ear Economics Economic ggplot2 function
沙发
kaifengedu 发表于 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这本书?



已有 2 人评分论坛币 学术水平 热心指数 收起 理由
qoiqpwqr + 20 补偿
耕耘使者 + 2 + 1 热心帮助其他会员

总评分: 论坛币 + 20  学术水平 + 2  热心指数 + 1   查看全部评分

使用道具

藤椅
firelife 发表于 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






已有 1 人评分论坛币 收起 理由
qoiqpwqr + 20 鼓励积极发帖讨论

总评分: 论坛币 + 20   查看全部评分

使用道具

板凳
耕耘使者 发表于 2012-1-28 14:58:05 |只看作者 |坛友微信交流群
firelife 发表于 2012-1-28 14:28
建议楼主看看R cookbook,里面的东西挺实用的
谢谢楼上朋友,您给出的是一般性的理论,能否针对我的具体问题给出具体的解答?

使用道具

报纸
firelife 发表于 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
已有 1 人评分学术水平 热心指数 收起 理由
耕耘使者 + 1 + 1 热心帮助其他会员

总评分: 学术水平 + 1  热心指数 + 1   查看全部评分

使用道具

地板
耕耘使者 发表于 2012-1-28 15:07:50 |只看作者 |坛友微信交流群
http://had.co.nz/
It is a gooe web for me.Many thanks!

使用道具

7
耕耘使者 发表于 2012-1-28 18:15:55 |只看作者 |坛友微信交流群
kaifengedu 发表于 2012-1-28 14:13
简单地理解,它是时间转换的一种格式,年代从1900年算起。
> as.POSIXlt("1900-01-02")$year
[1] 0
似乎还是有一点点问题,在书中作出了图,显示year(date)只有四个水平:1970,1980,1990,2000,而按您方法,显示的year(economics$date)却是1967,1967,1967,。。。,1968,1968,。。。。这是为什么?
时间转换.bmp

时间转换.bmp (949.27 KB)

时间转换.bmp

使用道具

8
kaifengedu 发表于 2012-1-28 19:22:52 |只看作者 |坛友微信交流群
这个我也没法明白,期待高人帮忙!
看一下下面的命令能否发现什么规律吗?
set.seed(1410) # Make the sample reproducible
dsmall <- economics[sample(nrow(economics), 5), ]
year <- function(x) as.POSIXlt(x)$year + 1900
qplot(unemploy / pop, uempmed, data = dsmall,
  geom = "path", colour = year(date)) + scale_area()
上述命令生成图1,

set.seed(1410) # Make the sample reproducible
dsmall <- economics[sample(nrow(economics), 100), ]
year <- function(x) as.POSIXlt(x)$year + 1900
qplot(unemploy / pop, uempmed, data = dsmall,
  geom = "path", colour = year(date)) + scale_area()

上述命令生成图2,
图1.bmp 图2.bmp

使用道具

9
firelife 发表于 2012-1-28 20:25:48 |只看作者 |坛友微信交流群
set.seed(1410) # Make the sample reproducible
dsmall <- economics[sample(nrow(economics), 5), ]
year <- function(x)  as.factor(as.POSIXlt(x)$year + 1900)
qplot(unemploy / pop, uempmed, data = dsmall,
  geom = c("path","point"), group=1,colour = year(date))

Rplot1.png



set.seed(1410) # Make the sample reproducible
dsmall <- economics[sample(nrow(economics), 100), ]
year <- function(x)  as.POSIXlt(x)$year + 1900
qplot(unemploy / pop, uempmed, data = dsmall,
  geom = c("path","point"), colour = year(date))+scale_area()
Rplot2.png


使用道具

10
firelife 发表于 2012-1-28 20:54:01 |只看作者 |坛友微信交流群
set.seed(1410) # Make the sample reproducible
dsmall <- economics[sample(nrow(economics), 100), ]
year <- function(x) as.POSIXlt(x)$year + 1900
qplot(unemploy / pop, uempmed, data = dsmall,
  geom = "path", colour = year(date)) + scale_colour_gradient(breaks = c(1970,1980,1990,2000))

Rplot3.png

使用道具

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-4-27 23:22