如何在R或Excel中对齐不同列的日期
我有一个变量数据集,每个变量都有不同的日期跨度。如下例所示(以500个案例中的前两个为例): 我想做的是让如何在R或Excel中对齐不同列的日期,r,excel,date,R,Excel,Date,我有一个变量数据集,每个变量都有不同的日期跨度。如下例所示(以500个案例中的前两个为例): 我想做的是让DatesV2中的日期与DatesV1中的日期对齐。这意味着DatesV2将从几个NA开始,直到日期对齐的行。像这样: DatesV1 DatesV2 ... ... 17/07/2001 NA 18/07/2001 NA 19/07/2001 19/07/2001 20/07/2001 20/07/2001 ... ... 在示例集中,
DatesV2
中的日期与DatesV1
中的日期对齐。这意味着DatesV2
将从几个NA
开始,直到日期对齐的行。像这样:
DatesV1 DatesV2
... ...
17/07/2001 NA
18/07/2001 NA
19/07/2001 19/07/2001
20/07/2001 20/07/2001
... ...
在示例集中,我有一个我正试图做的示例。对于我拥有的500个变量,我找不到一种快速的计算方法来在R或Excel中进行计算。
我试过这样的方法:
nhat<-which(Example$DatesV2[1]==Example$DatesV1)
nend<-which(Example$DatesV1[length(Example$DatesV1)-1]==Example$DatesV2)
Example$Apotelesma<- c(rep(NA,nhat-1),Example$DatesV2[1:nend],NA)
nhat这里有一个可能的解决方案,使用一些重新塑造。我用了一个简单的例子:
df = data.frame(DatesV1 = c("24/07/2001","25/07/2001","26/07/2001"),
DatesV2 = c("25/07/2001","26/07/2001","27/07/2001"),
DatesV3 = c("26/07/2001","27/07/2001","28/07/2001"),
stringsAsFactors = F)
library(tidyverse)
library(lubridate)
# update to date columns (only if needed)
df = df %>% mutate_all(dmy)
df %>%
gather() %>% # reshape dataset
mutate(id = value) %>% # use date values as row ids
spread(key, value) %>% # reshape again
select(-id) # remove ids
# DatesV1 DatesV2 DatesV3
# 1 2001-07-24 <NA> <NA>
# 2 2001-07-25 2001-07-25 <NA>
# 3 2001-07-26 2001-07-26 2001-07-26
# 4 <NA> 2001-07-27 2001-07-27
# 5 <NA> <NA> 2001-07-28
df=data.frame(DatesV1=c(“24/07/2001”、“25/07/2001”、“26/07/2001”),
日期v2=c(“2001年7月25日”、“2001年7月26日”、“2001年7月27日”),
日期v3=c(“2001年7月26日”、“2001年7月27日”、“2001年7月28日”),
(系数=F)
图书馆(tidyverse)
图书馆(lubridate)
#更新到日期列(仅在需要时)
df=df%>%mutate_all(dmy)
df%>%
聚集()%>%#重塑数据集
mutate(id=value)%>%#使用日期值作为行id
排列(键,值)%>%#再次重塑
选择(-id)#删除id
#日期1日期2日期3
# 1 2001-07-24
# 2 2001-07-25 2001-07-25
# 3 2001-07-26 2001-07-26 2001-07-26
# 4 2001-07-27 2001-07-27
# 5 2001-07-28
这里有一个可能的解决方案,使用一些重新成形。我用了一个简单的例子:
df = data.frame(DatesV1 = c("24/07/2001","25/07/2001","26/07/2001"),
DatesV2 = c("25/07/2001","26/07/2001","27/07/2001"),
DatesV3 = c("26/07/2001","27/07/2001","28/07/2001"),
stringsAsFactors = F)
library(tidyverse)
library(lubridate)
# update to date columns (only if needed)
df = df %>% mutate_all(dmy)
df %>%
gather() %>% # reshape dataset
mutate(id = value) %>% # use date values as row ids
spread(key, value) %>% # reshape again
select(-id) # remove ids
# DatesV1 DatesV2 DatesV3
# 1 2001-07-24 <NA> <NA>
# 2 2001-07-25 2001-07-25 <NA>
# 3 2001-07-26 2001-07-26 2001-07-26
# 4 <NA> 2001-07-27 2001-07-27
# 5 <NA> <NA> 2001-07-28
df=data.frame(DatesV1=c(“24/07/2001”、“25/07/2001”、“26/07/2001”),
日期v2=c(“2001年7月25日”、“2001年7月26日”、“2001年7月27日”),
日期v3=c(“2001年7月26日”、“2001年7月27日”、“2001年7月28日”),
(系数=F)
图书馆(tidyverse)
图书馆(lubridate)
#更新到日期列(仅在需要时)
df=df%>%mutate_all(dmy)
df%>%
聚集()%>%#重塑数据集
mutate(id=value)%>%#使用日期值作为行id
排列(键,值)%>%#再次重塑
选择(-id)#删除id
#日期1日期2日期3
# 1 2001-07-24
# 2 2001-07-25 2001-07-25
# 3 2001-07-26 2001-07-26 2001-07-26
# 4 2001-07-27 2001-07-27
# 5 2001-07-28
如果您愿意,这是一种丑陋/混乱的方法,但它可以完成工作。任何更快、更整洁的方式都会更好
n<-nrow(DataAlignment)
Newdata<-matrix(0,5148,ncol(DataAlignment))
loops<-ncol(DataAlignment)-1
for(i in 1:loops){
nhat<-which(DataAlignment[1,i+1]==DataAlignment[,1]) #finds the position of the first date in column 2 according to the first column
nend<-which(DataAlignment[n,1]==DataAlignment[,i+1]) #finds the position of last date in col 2 according to the first column
if(nhat==1 | nend != 5148){ #takes into account when they start at the same time but end in different dates
Newdata[,i+1]<-c(DataAlignment[c(1:nend),i+1],rep(NA,n-nend))
}
else{if(nhat==1| nend==5148){Newdata[,i+1]<-c(DataAlignment[,i+1])} #this takes account when they start and end at the same time
else{if(nhat!=1){
Newdata[,i+1]<-c(rep(NA,nhat-1),DataAlignment[c(1:nend),i+1])}}} #creates the new data
}
n如果你愿意的话,这是一种丑陋/混乱的方法,但它可以完成工作。任何更快、更整洁的方式都会更好
n<-nrow(DataAlignment)
Newdata<-matrix(0,5148,ncol(DataAlignment))
loops<-ncol(DataAlignment)-1
for(i in 1:loops){
nhat<-which(DataAlignment[1,i+1]==DataAlignment[,1]) #finds the position of the first date in column 2 according to the first column
nend<-which(DataAlignment[n,1]==DataAlignment[,i+1]) #finds the position of last date in col 2 according to the first column
if(nhat==1 | nend != 5148){ #takes into account when they start at the same time but end in different dates
Newdata[,i+1]<-c(DataAlignment[c(1:nend),i+1],rep(NA,n-nend))
}
else{if(nhat==1| nend==5148){Newdata[,i+1]<-c(DataAlignment[,i+1])} #this takes account when they start and end at the same time
else{if(nhat!=1){
Newdata[,i+1]<-c(rep(NA,nhat-1),DataAlignment[c(1:nend),i+1])}}} #creates the new data
}
nI获取此消息:df=df%>%mutate_all(dmy)警告消息:1:所有格式解析失败。找不到任何格式。2:无法分析所有格式。找不到任何格式代码>同时:class(df$DatesV1)[1]“Date”
dmy(df$DatesV2)
为所有条目返回NA
。我假设您成功运行了我的示例,但您得到的是完整数据集,对吗?要么您有其他非日期变量,要么您有其他日期格式。是!你的例子运行得很好!当我展开它时,我的案例中出现了错误。如果您的列已经是date
,那么您可能不需要这段代码。它们有相同的格式吗dmy
是“日-月-年”。我得到以下消息:df=df%>%mutate\u all(dmy)警告消息:1:所有格式解析失败。找不到任何格式。2:无法分析所有格式。找不到任何格式代码>同时:class(df$DatesV1)[1]“Date”
dmy(df$DatesV2)
为所有条目返回NA
。我假设您成功运行了我的示例,但您得到的是完整数据集,对吗?要么您有其他非日期变量,要么您有其他日期格式。是!你的例子运行得很好!当我展开它时,我的案例中出现了错误。如果您的列已经是date
,那么您可能不需要这段代码。它们有相同的格式吗<代码>dmy
是“日-月-年”。