如何替换R中的列?日期的奇怪行为
我正在尝试将不常见的日期格式转换为标准日期。基本上我有一个数据集,其中包含半年频率的周期,格式如下:206 表示 2006 年下半年,106 表示上半年,依此类推。为了将其重新排列为2006-06-01分别为2006-01-01,我写了一个小函数:
period2date = function(period)
{
check=list()
check=strsplit(as.character(period),split="")
x=as.numeric(check[[1]][1])
p=ifelse( x >= 2,6,1)
x=2
out=paste(x,"0",check[[1]][2],check[[1]][3],"-",p,"-1",sep="")
out=as.Date(out)
return(out)
}
你现在可能会笑:)。无论如何,该功能有效,但问题来了。我想将此函数应用于 data.frame 的时间列。我尝试了以下操作:
as.data.frame(lapply(mydf$period,period2date))
返回的结果最接近我想要的结果: 结构.13665..类....日期.. 1 2006-06-01
等等..显然我很想保留我的专栏的名称 - 或者更好的是只需将新格式化的日期添加到我原来的 df.1 中。另外我尝试过:
sapply(mydf$period,period2date) # with results equal to the line below
unlist(lapply(mydf$period,period2date))
[1] 13300 13514 13665
我想做的就是将不常见的 206 等格式更改为 2006-06-01 (有效)并向 mydf 添加一列(无效),
谢谢您的任何建议进步!
I am trying to convert a uncommon date format into a standard date. Basically I have a dataset that contains a period with semiannual frequency formatted like: 206 denoting the second half of 2006, 106 denoting the first half and so forth. In order to rearrange it to 2006-06-01 respectively 2006-01-01, i have written a small function:
period2date = function(period)
{
check=list()
check=strsplit(as.character(period),split="")
x=as.numeric(check[[1]][1])
p=ifelse( x >= 2,6,1)
x=2
out=paste(x,"0",check[[1]][2],check[[1]][3],"-",p,"-1",sep="")
out=as.Date(out)
return(out)
}
you may laugh now :) . Anyway, that function works and here comes the problem. I want to apply this function to the time column of data.frame. I tried the following:
as.data.frame(lapply(mydf$period,period2date))
which returned the result closest to what I want:
structure.13665..class....Date..
1 2006-06-01
and so forth.. obviously i´d love to keep the name of my column – or even better just add the newly formatted date to my original df. Plus I tried:
sapply(mydf$period,period2date) # with results equal to the line below
unlist(lapply(mydf$period,period2date))
[1] 13300 13514 13665
All I want to do is change the uncommon 206 etc. format to 2006-06-01 (which works) and add a column to mydf (which does not work)
thx for any suggestions in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
R 将日期存储为数字,所以我认为您会遇到一些奇怪的行为,因为您正在对日期输出进行操作(即,将日期放回到矩阵中,这使它们显示为数字他们确实是)。相反,您应该显式地将 data.frame 与
data.frame()
一起使用。另外,如果您使用矢量化操作,您可能会节省一些时间(我认为apply
系列仍然使用循环):您还可以通过替换附加期间/日期列来使此更清晰。
R
stores dates as numbers, so I think you're getting some wacky behavior because you're operating on the date output (i.e., putting the dates back into a matrix, which makes them appear as the numbers they really are). Instead, you should explicitly use a data.frame withdata.frame()
. Also, you may save some time if you use vectorized operations (I think theapply
family still uses loops):You can make this cleaner by replacing vice appending the period/date column, also.
这很奇怪......:
返回“2006-06-01”“2006-01-01”等。我很震惊,因为 period2date 函数已经包含 as.Date()。这是我的问题的解决方案,但我不完全理解......
This is strange...:
returns "2006-06-01" "2006-01-01" etc. I am stunned because the period2date function already contains as.Date(). This is a solution to my problem, but I don´t understand it completely...