使用unlist将日期型数据的列表转换为向量时,出现的异常
在使用unlist函数,将日期型的列表,转换为向量时,不会得到期望的结果,如下:
> dateLst <- list(Sys.Date())
> dateLst
[[1]]
[1] "2015-08-11" > dateVector <- unlist(dateLst)
> dateVector
[1] 16658 同样的原因,在使用sapply(X, FUN, ..., simplify = TRUE, USE.NAMES = TRUE)时,如果X为日期型向量,FUN返回日期,那么结果不会是预期的日期型向量,如下:
> date.vector.string <- c("20010101")
> date.vector <- sapply(date.vector.string, get.date)
> date.vector
20010101
11323 其中:
# Get date from date string. Assumed the format of date string is like "20011230"
#
# Args:
# date.string: date string like "20011230"
#
# Returns:
# date.
get.date <- function(date.string){
# deal with the Error in strptime(x, format, tz = "GMT") : input string is too long
if(nchar(date.string) != 8)
return(NA)
date <- try(as.Date(date.string, "%Y%m%d"))
return(date)
} sapply的结果,看起来可以认为是先调用lapply,得到的list后,调用unlist,试图得到与date.vector.string同类型的结果即向量。 考虑到日期型列表转换为向量的麻烦,最好的方法就是不要出现日期型列表,尽量用向量。 关于对
> dateVector <- unlist(dateLst)
> dateVector
[1] 16658 的解释,可以参加下列代码:
> as.Date(16658, origin = "1970-01-01")
[1] "2015-08-11" 看来unlist时默认了一个时间点1970-01-01.
使用unlist将日期型数据的列表转换为向量时,出现的异常的更多相关文章
- 用R语言提取数据框中日期对应年份(列表转矩阵)
用R语言提取数据框中日期对应年份(列表转矩阵) 在数据处理中常会遇到要对数据框中的时间做聚类处理,如从"%m/%d/%Y"中提取年份. 对应操作为:拆分成列表——列表转矩阵——利用 ...
- pandas中对日期型数据进行处理
因为数据不方便展示,直接上代码. 将字符串转为datetime64[ns]格式: pd.to_datetime('2019-12-20') or pd.to_datetime('20191220') ...
- 对象列表转换为DataTable或DataTable转换为对象列表.
/**********************************************************************************/ // 说明: 数据转换工具. ...
- python中循环删除列表中元素时的坑!
循环删除列表中元素时千万别用正序遍历,一定要用反序遍历! 废话不多说,先上案例代码: def test(data): for i in data: data.remove(i) return data ...
- (一)求 int 型数据在内存中存储时 1 的个数
题目:求 int 型数据在内存中存储时 1 的个数 描述:输入一个 int 型数据,计算出该 int 型数据在内存中存储时 1 的个数 运行时间限制: 10 sec 内存限制:128 MByte 输入 ...
- 华为机试 求int型数据在内存中存储时1的个数
题目描述 输入一个int型的正整数,计算出该int型数据在内存中存储时1的个数. 输入描述: 输入一个整数(int类型) 输出描述: 这个数转换成2进制后,输出1的个数 输入 5 输出 2 普通运算方 ...
- 求int型数据在内存中存储时1的个数
1.求int型数据在内存中存储时1的个数 输入一个int型数据,计算出该int型数据在内存中存储时1的个数. 我们非常easy想到例如以下方法: #include <iostream> u ...
- php实现 求int型数据在内存中存储时1的个数(函数都可自己实现)
php实现 求int型数据在内存中存储时1的个数(函数都可自己实现) 一.总结 一句话总结:函数我们自己都可以实现,尤其是很多基础函数,没有工具的时候自己写. 1.php进制转换函数? base_co ...
- python3 列表转换为字符串
join将列表转换为字符串 list1 = ["张三","李四","王五"] a1 = ','.join(list1) print(a1) ...
随机推荐
- charles 4.x 破解版安装 以及使用
下载地址 https://pan.baidu.com/s/1dFvYM7B 破解方法 未破解的情况下,每30分钟会弹出一个提示,然后关闭软件 将压缩包内的 charles.jar 复制到安装目录下,替 ...
- PowerBuilder -- Len(), LenA() 与 String, Blob
使用的是Powerbuilder12.5与Powerbuild9 不太一样 函数 String Blob Len() 返回字符数 返回字符数对应的字节数 LenA() 返回字节数 返回字符数对应的字节 ...
- idea创建普通java项目以及maven创建项目过程(转)
1. idea创建一个普通项目流程 http://blog.csdn.net/testcs_dn/article/details/52303941 2. idea创建maven项目流程 http:// ...
- 九度OJ 1161:Repeater(复制器) (递归)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:1449 解决:508 题目描述: Harmony is indispensible in our daily life and no one ...
- Kubernetes TensorFlow 默认 特定 集群管理器
Our goal is to foster an ecosystem of components and tools that relieve the burden of running applic ...
- python获取当前的时间
打印出当前的年月日时分秒 print(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))) 2018-09-05 09:39: ...
- Linux下/usr/bin/python被删除的后果
可能部分的人使用linux都有直接root登陆的习惯,这有很大的便利性,因为很多的命令不需要使用sudo请求root权限.但是使用root权限,所有的命令都会立即被执行,即使这个命令是对系统有害处的. ...
- Why use async requests instead of using a larger threadpool?(转载)
问: During the Techdays here in the Netherlands Steve Sanderson gave a presentation about C#5, ASP.NE ...
- LeetCode:安排工作以达到最大收益【455】
LeetCode:安排工作以达到最大收益[455] 题目描述 有一些工作:difficulty[i] 表示第i个工作的难度,profit[i]表示第i个工作的收益. 现在我们有一些工人.worker[ ...
- mini2440 uboot烧写uImage
mini2440下烧写u-boot后,就可以用u-boot烧写linux内核了. 安装mkimage工具: apt-get install u-boot-tools 解压缩官方mini2440 lin ...