0

一意の各 ID に、fruit=='apple' の first.date に基づいて、first.date の同じ列の値を与えたいと考えています。

これは私が持っているものです:

 names      dates  fruit first.date
1   john 2010-07-01   kiwi       <NA>
2   john 2010-09-01  apple 2010-09-01
3   john 2010-11-01 banana       <NA>
4   john 2010-12-01 orange       <NA>
5   john 2011-01-01  apple 2010-09-01
6   mary 2010-05-01 orange       <NA>
7   mary 2010-07-01  apple 2010-07-01
8   mary 2010-07-01 orange       <NA>
9   mary 2010-09-01  apple 2010-07-01
10  mary 2010-11-01  apple 2010-07-01

これは私が欲しいものです:

 names      dates  fruit first.date
1   john 2010-07-01   kiwi 2010-09-01
2   john 2010-09-01  apple 2010-09-01
3   john 2010-11-01 banana 2010-09-01
4   john 2010-12-01 orange 2010-09-01
5   john 2011-01-01  apple 2010-09-01
6   mary 2010-05-01 orange 2010-07-01
7   mary 2010-07-01  apple 2010-07-01
8   mary 2010-07-01 orange 2010-07-01
9   mary 2010-09-01  apple 2010-07-01
10  mary 2010-11-01  apple 2010-07-01

これは私の悲惨な試みです:

getdates$first.date[is.na]<-getdates[getdates$first.date & getdates$fruit=='apple',]

前もって感謝します

再現可能なDF

names<-as.character(c("john", "john", "john", "john", "john", "mary", "mary","mary","mary","mary"))
dates<-as.Date(c("2010-07-01",  "2010-09-01", "2010-11-01", "2010-12-01", "2011-01-01", "2010-05-01", "2010-07-01", "2010-07-01",  "2010-09-01",  "2010-11-01"))
fruit<-as.character(c("kiwi","apple","banana","orange","apple","orange","apple","orange", "apple", "apple")) 
first.date<-as.Date(c(NA, "2010-09-01",NA,NA, "2010-09-01", NA, "2010-07-01", NA, "2010-07-01","2010-07-01"))
getdates<-data.frame(names,dates,fruit, first.date)
4

1 に答える 1