基于日期的单独数据帧匹配因子级别

问题描述 投票:0回答:1

我确信有一个简单的解决方案(我似乎无法找到一个有效的cut.date解决方案)。我有一个数据框,每月的价值被分类为离散的类别。

Monthly <-as.data.frame(seq(as.Date("2010/1/1"), as.Date("2016/12/31"), "months"))
colnames(Monthly) [1] <- "Date"
Monthly$Category <- c(rep("A", times = 7), rep("B", times = 12),
rep("C", times = 12),rep("B", times = 12), rep("C", times = 12),
 rep("B", times = 12),rep("C", times = 12), rep("A", times = 5))

我有一个单独的数据框,其中包含测量值和样本的日期(在与每月值相同的时间范围内拍摄,但在同一天收集了大量样本)。

Daily <- as.data.frame(c(rep(as.Date("2010/03/12"), times = 200),
rep(as.Date("2010/08/24"), times = 200), rep(as.Date("2011/03/12"), times = 200),
rep(as.Date("2011/08/24"), times = 200),rep(as.Date("2012/03/12"), times = 200),
rep(as.Date("2012/08/24"), times = 200),rep(as.Date("2013/03/12"), times = 200),
rep(as.Date("2013/08/24"), times = 200),rep(as.Date("2014/03/12"), times = 200),
rep(as.Date("2014/08/24"), times = 200),rep(as.Date("2015/03/12"), times = 200),
rep(as.Date("2015/08/24"), times = 200),rep(as.Date("2016/03/12"), times = 157)))

Values <- as.data.frame(matrix(sample(0:200,255.7*10, replace=TRUE),ncol=1))
df <- data.frame(Values,Daily)
colnames(df) <- c("values","daily")

我想在df中创建一个包含相应月份类别的新列。因此,例如,根据“每月”数据框架,2010年3月可归类为A类。因此,在df的新列中,我希望2010年3月拍摄的所有样本都具有“A”值。

r date match
1个回答
1
投票

使用merge尝试此解决方案:

out<-merge(df,Monthly,by.x=substr(as.character("daily"),1,7),by.y=substr(as.character("Date"),1,7))

你的输出:

head(out,10)
        daily values Category
1  2010-01-01     29        A
2  2010-02-01    132        A
3  2010-03-01     60        A
4  2010-04-01     96        A
5  2010-05-01     69        A
6  2010-06-01     18        A
7  2010-07-01     47        A
8  2010-08-01    133        B
9  2010-09-01    121        B
10 2010-10-01     73        B

以不同的方式

数据帧之间的关键创建

df$id<-substr(as.character(df$daily),1,7)
Monthly$id<-substr(as.character(Monthly$Date),1,7)

加入使用id

library(plyr)
out<-join(df, Monthly, by="id",type="full")

你的输出

head(out[,c(2,1,5)])
daily values Category
1 2010-03-12     94        A
2 2010-03-12     73        A
3 2010-03-12    132        A
4 2010-03-12     94        A
5 2010-03-12    164        A
6 2010-03-12     81        A
© www.soinside.com 2019 - 2024. All rights reserved.