查找唯一 ID 的最早日期,然后将单独的因子设置为值
Find earliest date for a unique ID then set separate factor to value
我需要确定每个唯一ID(n=3127)的最早日期并为其分配特定值(30),但如果日期不是唯一ID的最早日期,那么我需要设置另一个因素的价值。以下是数据:
Date ID Count
1/1/2020 1 -37
1/13/2020 1 12
2/1/2020 1 18
3/4/2020 2 470
3/24/2020 2 20
4/1/2020 2 6
最终数据帧:
1/1/2020 1 30
1/13/2020 1 12
2/1/2020 1 18
3/4/2020 2 30
3/24/2020 2 20
4/1/2020 2 6
我有一些代码最初可以工作,但出了点问题:
df$Temp1=c(NA,df$ID[2:nrow(df)-1])
df$Count=ifelse(df$ID==df$Temp1, Records3$Count, NA)
df$Count=ifelse((is.na(df$Count)==TRUE), 30, df$Count)
如有任何建议,我们将不胜感激。干杯,道格
逻辑不清楚。或许,我们可以将 'ID' 和 replace
第一个元素分组为 30
library(dplyr)
library(lubridate)
df1 %>%
mutate(Date = mdy(Date)) %>%
arrange(ID, Date) %>%
group_by(ID) %>%
mutate(Count = replace(Count, 1, 30)) %>%
ungroup
# A tibble: 6 x 3
# Date ID Count
# <date> <int> <dbl>
#1 2020-01-01 1 30
#2 2020-01-13 1 12
#3 2020-02-01 1 18
#4 2020-03-04 2 30
#5 2020-03-24 2 20
#6 2020-04-01 2 6
或者使用base R
,order
数据先由'Date'和'ID',然后根据逻辑向量赋值'Count'用 duplicated
到 30
创建
df2 <- df1[order(df1$ID, as.Date(df1$Date, "%m/%d/%Y")),]
df2$Count[!duplicated(df1$ID)] <- 30
数据
df1 <- structure(list(Date = c("1/1/2020", "1/13/2020", "2/1/2020",
"3/4/2020", "3/24/2020", "4/1/2020"), ID = c(1L, 1L, 1L, 2L,
2L, 2L), Count = c(-37L, 12L, 18L, 470L, 20L, 6L)), class = "data.frame",
row.names = c(NA,
-6L))
您可以转换为 Date
class,并且每个 ID
最早将 Count
更改为 Date
到 30。
library(dplyr)
df %>%
mutate(Date = as.Date(Date, '%m/%d/%Y')) %>%
group_by(ID) %>%
mutate(Count = replace(Count, which.min(Date), 30))
# Date ID Count
# <date> <int> <dbl>
#1 2020-01-01 1 30
#2 2020-01-13 1 12
#3 2020-02-01 1 18
#4 2020-03-04 2 30
#5 2020-03-24 2 20
#6 2020-04-01 2 6
我需要确定每个唯一ID(n=3127)的最早日期并为其分配特定值(30),但如果日期不是唯一ID的最早日期,那么我需要设置另一个因素的价值。以下是数据:
Date ID Count
1/1/2020 1 -37
1/13/2020 1 12
2/1/2020 1 18
3/4/2020 2 470
3/24/2020 2 20
4/1/2020 2 6
最终数据帧:
1/1/2020 1 30
1/13/2020 1 12
2/1/2020 1 18
3/4/2020 2 30
3/24/2020 2 20
4/1/2020 2 6
我有一些代码最初可以工作,但出了点问题:
df$Temp1=c(NA,df$ID[2:nrow(df)-1])
df$Count=ifelse(df$ID==df$Temp1, Records3$Count, NA)
df$Count=ifelse((is.na(df$Count)==TRUE), 30, df$Count)
如有任何建议,我们将不胜感激。干杯,道格
逻辑不清楚。或许,我们可以将 'ID' 和 replace
第一个元素分组为 30
library(dplyr)
library(lubridate)
df1 %>%
mutate(Date = mdy(Date)) %>%
arrange(ID, Date) %>%
group_by(ID) %>%
mutate(Count = replace(Count, 1, 30)) %>%
ungroup
# A tibble: 6 x 3
# Date ID Count
# <date> <int> <dbl>
#1 2020-01-01 1 30
#2 2020-01-13 1 12
#3 2020-02-01 1 18
#4 2020-03-04 2 30
#5 2020-03-24 2 20
#6 2020-04-01 2 6
或者使用base R
,order
数据先由'Date'和'ID',然后根据逻辑向量赋值'Count'用 duplicated
到 30
df2 <- df1[order(df1$ID, as.Date(df1$Date, "%m/%d/%Y")),]
df2$Count[!duplicated(df1$ID)] <- 30
数据
df1 <- structure(list(Date = c("1/1/2020", "1/13/2020", "2/1/2020",
"3/4/2020", "3/24/2020", "4/1/2020"), ID = c(1L, 1L, 1L, 2L,
2L, 2L), Count = c(-37L, 12L, 18L, 470L, 20L, 6L)), class = "data.frame",
row.names = c(NA,
-6L))
您可以转换为 Date
class,并且每个 ID
最早将 Count
更改为 Date
到 30。
library(dplyr)
df %>%
mutate(Date = as.Date(Date, '%m/%d/%Y')) %>%
group_by(ID) %>%
mutate(Count = replace(Count, which.min(Date), 30))
# Date ID Count
# <date> <int> <dbl>
#1 2020-01-01 1 30
#2 2020-01-13 1 12
#3 2020-02-01 1 18
#4 2020-03-04 2 30
#5 2020-03-24 2 20
#6 2020-04-01 2 6