楼主: bob_hai
2708 9

[问答] 关于R的两个问题求助 [推广有奖]

  • 2关注
  • 1粉丝

VIP

博士生

26%

还不是VIP/贵宾

-

威望
0
论坛币
342 个
通用积分
0
学术水平
6 点
热心指数
1 点
信用等级
0 点
经验
23225 点
帖子
106
精华
0
在线时间
414 小时
注册时间
2008-3-14
最后登录
2014-6-25

楼主
bob_hai 发表于 2011-8-9 11:42:04 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
使用R 没多久 有两个关于R的问题不知道有没有高手能解答一下
一个是关于时间的问题, 我有一个时间变量,用as.POSIXct函数转化后
变成2008-12-01 18:22:54 的格式,想问一下如何能方便的取出其中的年,
月,日,时,分,秒
第二个问题是关于查看函数体的,在使用包randomForest时有一个importance函数,怎样
才能看到它的源代码,直接输importance, 得到的是
function (x, ...)
UseMethod("importance")
<environment: namespace:randomForest>
接下来就不知道该怎么办了

二维码

扫码加我 拉你入群

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

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

关键词:randomForest importance function import Forest function 源代码 如何

沙发
trier2006 发表于 2011-8-9 12:13:14
友情帮顶!
最好的医生是自己,最好的药物是时间……

藤椅
berryice 发表于 2011-8-9 13:35:29
LZ能把第一个问题中的具体时间变量给出来吗?
关于第二个问题的代码:
library(randomForest)
getS3method("importance","randomForest")
Bless

板凳
zhangyangsmith 发表于 2011-8-9 16:09:54
If you want to extract the year, month, ... from the data POSIXlt class may be more convenient. Use as.POSIXlt() to convert to this class then use varName$year+1900, varName$month, varName$mday, varName$hour, varName$min and varName$sec to extract the year, month, day of month, hour, minute and second of the data/time. (Here varName is the name of the POSIXlt variable).

报纸
bob_hai 发表于 2011-8-9 16:54:47
非常感谢上面两位,终于都弄清楚了。

地板
bob_hai 发表于 2011-8-10 13:52:02
zhangyangsmith 发表于 2011-8-9 16:09
If you want to extract the year, month, ... from the data POSIXlt class may be more convenient. Use  ...
谢谢你的解答,我在实际操作中发现个问题如果如下定义
x <- data.frame(Date=as.POSIXlt(Time),.....)
其中 variable Time 是格式为"2008-09-10 10:12:33"的字符串
在这种情况下,似乎就没办法对Date来取year,month......了,
不知道有没有什么办法来解决, 谢谢

7
zhangyangsmith 发表于 2011-8-10 15:27:33
There is a mistake in my last post: to extract month one should use varName$mon+1 instead of varName$mon. Sorry for that. See the R help DateTimeClasses {base}:
Details

There are two basic classes of date/times. Class "POSIXct" represents the (signed) number of seconds since the beginning of 1970 (in the UTC timezone) as a numeric vector. Class "POSIXlt" is a named list of vectors representing

sec
0–61: seconds
min
0–59: minutes
hour
0–23: hours
mday
1–31: day of the month
mon
0–11: months after the first of the year.
year
years since 1900.
wday
0–6 day of the week, starting on Sunday.
yday
0–365: day of the year.
isdst
Daylight Savings Time flag. Positive if in force, zero if not, negative if unknown.
Not quite sure what you meant in the last post. The function as.POSIXlt() will convert the string into a list which represents date and time. Here is my test (R-2.12.2):
  1. > # the string of the date/time
  2. > Time <- "2008-09-10 10:12:33"
  3. > # type of variable "Time"
  4. > class(Time)
  5. [1] "character"
  6. > # as.POSIXlt convert the above string into an instant of class POSIXlt
  7. > Date <- as.POSIXlt(Time)
  8. > # type of variable "Date"
  9. > class(Date)
  10. [1] "POSIXlt" "POSIXt"
  11. > # year
  12. > Date$year + 1900
  13. [1] 2008
  14. > # month
  15. > Date$mon + 1
  16. [1] 9
  17. > # day of month
  18. > Date$mday
  19. [1] 10
  20. > # hour
  21. > Date$hour
  22. [1] 10
  23. > # minute
  24. > Date$min
  25. [1] 12
  26. > # second
  27. > Date$sec
  28. [1] 33
复制代码

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

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

8
bob_hai 发表于 2011-8-10 16:11:13
zhangyangsmith 发表于 2011-8-10 15:27
There is a mistake in my last post: to extract month one should use varName$mon+1 instead of varName ...
我的意思是如果用as.POSIXlt()定义得到时间变量,在现实中不会单独使用
它往往成为一个数据集中的一个时间index,举个例子
data <- data.frame(Time,Num), 其中的Time 属于 class POSIXlt, Num 是每秒细菌繁殖数量

data
Time                           Num
2008-09-10 10:12:33    20
2008-09-10 10:12:34    40
      .                            .
      .                            .
如果我要选取第12分钟的所有数据
data[data$Time$min==12,]
这是我希望能实现的语句,但是这条命令是错误的,我的问题是
在这种情况下怎么取min, 谢谢

9
zhangyangsmith 发表于 2011-8-10 16:49:53
It seems the data.frame() function has changed the type of POSIXlt instants while constructing the data. See the following test:
  1. > Time <- as.POSIXlt(c("2008-09-10 10:12:33", "2008-09-10 10:12:34"))
  2. > Num <- c(20, 40)
  3. > data <- data.frame(Time, Num)
  4. > class(data$Time)
  5. [1] "POSIXct" "POSIXt"
复制代码
So you will need to use the as.POSIXlt() again:
  1. > data[as.POSIXlt(data$Time)$min == 12, ]
  2.                  Time Num
  3. 1 2008-09-10 10:12:33  20
  4. 2 2008-09-10 10:12:34  40
复制代码
已有 2 人评分经验 论坛币 学术水平 热心指数 信用等级 收起 理由
guo.bailing + 100 + 3 + 3 + 3 精彩帖子
yahoocom + 40 鼓励积极发帖讨论

总评分: 经验 + 100  论坛币 + 40  学术水平 + 3  热心指数 + 3  信用等级 + 3   查看全部评分

10
bob_hai 发表于 2011-8-11 09:25:26
zhangyangsmith 发表于 2011-8-10 16:49
It seems the data.frame() function has changed the type of POSIXlt instants while constructing the d ...
谢谢, 只能这么变通一下了

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

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2026-1-2 23:59