I'm trying to add ID codes to a dataframe (creel), which is a time series of shellfish landings from 2000 to 2015. The first few columns are given below:
head(creel,10)
week year boat fID
1 2000-W01 2000 Mousa NA
2 2000-W01 2000 Yell NA
3 2000-W01 2000 Foula NA
4 2000-W01 2000 Foula NA
5 2000-W02 2000 Foula NA
6 2000-W02 2000 Papa Stour NA
7 2000-W02 2000 Fetlar NA
8 2000-W02 2000 Unst NA
9 2000-W03 2000 Foula NA
10 2000-W03 2000 Fair Isle NA
...
The IDs are for boat owners, which change over time. I have details for who owned which boat and when, and have created unique codes to add to the "fID" column (which I created and filled with NAs). For the purposes of this, let's say "aa" is the ID for the Mousa, "ab" for Yell, "ac" for Foula etc.
The week vector was created from actual dates, using the ISOweek function. The week vector is an ordered factor, so R knows the correct order from start to finish in the time series:
creel$week <- as.ordered(creel$week)
#Levels:2000-W01 < 2000-W02 < 2000-W03 < 2000-W04 < 2000-W05<...<2015-W53
I have tried to add the unique fID codes for one boat to begin with, but it did not work:
creel$fID[which(creel$boat=="Mousa" & creel$week=>"2004-W53" & creel$week=<"2015-W53"),] <- as.factor("aa")
"aa" is the code I'd like to assign in the fID vector, only between weeks 2004-W53 and 2015-W53. I wasn't sure if R would recognise > or < when using with the weeks - I did find that unclass(creel$week) gives actual values for week numbers which could maybe be used instead.
I also tried using ifelse, but only for boats where the owner did not change throughout the entire dataset (in which case the week is irrelevant). Something like this, (also didn't work!):
creel$fID <- ifelse(creel$boat=="Unst", as.factor(creel$fID=="ad"), NA)
The dataset is very large, although I'm happy to do each boat individually if it's easier.
Aucun commentaire:
Enregistrer un commentaire