Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在R中更改为年-月-日格式_R_Date - Fatal编程技术网

如何在R中更改为年-月-日格式

如何在R中更改为年-月-日格式,r,date,R,Date,我的数据如下所示:“19970325”“19970325”“19970422”“19970516” 我想转换为:1997-03-251977-03-251997-04-22…. 我发现 df[,2]这对我很有效 as.Date(as.character(df1$date), format='%Y%m%d') #[1] "1997-01-01" "1997-01-18" "1997-08-02" "1997-12-12" "1997-01-01" #[6] "1997-01-13" "199

我的数据如下所示:
“19970325”“19970325”“19970422”“19970516”

我想转换为:
1997-03-251977-03-251997-04-22….

我发现
df[,2]这对我很有效

 as.Date(as.character(df1$date), format='%Y%m%d')
 #[1] "1997-01-01" "1997-01-18" "1997-08-02" "1997-12-12" "1997-01-01"
 #[6] "1997-01-13" "1997-01-01" "1997-01-01" "1997-01-01" "1997-01-01"
 #[11] "1997-01-11" "1997-03-15" "1997-04-16" "1997-04-24"
更新 如果需要删除重复的ID,一个选项是
duplicated
。下面的代码给出了每个重复ID的第一行

 df1[!duplicated(df1$id),]
 #    id     date Amount
 #1    4 19970101  29.33
 #5   21 19970101  63.34
 #7   50 19970101   6.79
 #8   71 19970101  13.97
 #9   86 19970101  23.94
 #10 111 19970101  35.99
假设,如果需要获得每个id的“金额”之和,请使用一个“按组聚合”函数

  aggregate(Amount~id, df1, sum)
数据
df1我会使用
lubridate
install.library(“lubridate”)


尝试
v1错误:根据示例,在“df[,2]中出现意外的数字常量,前提是我没有收到任何错误。我尝试从表中转换列(我使用了CDNOW_示例)。我不知道
CDNOW_示例
是什么。请使用
dput
的输出用小示例更新您的帖子,即
dput(droplevels(head(yourdata)))
  aggregate(Amount~id, df1, sum)
 df1 <- structure(list(id = c(4L, 4L, 4L, 4L, 21L, 21L, 50L, 71L, 86L, 
 111L, 111L, 111L, 111L, 111L), date = c(19970101L, 19970118L, 
 19970802L, 19971212L, 19970101L, 19970113L, 19970101L, 19970101L, 
 19970101L, 19970101L, 19970111L, 19970315L, 19970416L, 19970424L
 ), Amount = c(29.33, 29.73, 14.96, 26.48, 63.34, 11.77, 6.79, 
 13.97, 23.94, 35.99, 32.99, 77.96, 59.3, 134.98)), .Names = c("id", 
 "date", "Amount"), class = "data.frame", row.names = c("1", "2", 
 "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14"))
library(lubridate)
ymd("20110604")

## [1] "2011-06-04 UTC"