I am a R beginner and currently facing a problem I can't conceptualize for now.
I have looked several related posts but have not find a specific answer except there
:
Aggregating rows with same Ids and retaining only unique entries in R
but my problem is a bit different.
Here's the structure of the initial df I wanna use :
sta_RHP_metho (3528,4) the variables are :
- "code.sandre" witch is the ID i'll use
- "CodeOpera" a unique id witch is related to "code.sandre"
- "Methode.de.peche" a character vector
- "year"
In that df there's as much rows as unique "CodeOpera" (3528). There are several "CodeOpera" by id/"code.sandre" and there are 180 code.sandre
What i want to get is a df with a unique row by "code.sandre" and the "Methode.de.peche" character value for each year.
I almost got that by processing the following code :
x2<-melt(sta_RHP_metho,c("code.sandre","CodeOpera","year"),"Methode.de.peche")
x3<-as.data.frame(dcast(x2,code.sandre + CodeOpera ~ year))
But I still have several as much rows as unique "CodeOpera" (3528) and as I said I don't know how to get a unique rox by ID.
A thing to notice is that it's possible to have several "Methode.de.peche" by year so i may need to concatenate returned values in some case.
Hope my explanations are clear.
Comments will be greatly appreciated ;)
Cheers.
Tristan
Thank you @ANG. Here's minimal reproducible example:
1/The dataframe I got after my melt/dcast operation :
code_sandre<-c("A","A","A","B","B","C","D")
year1<-c("a",NA,"a","b",NA,"c","b")
year2<-c("a","b",NA,"b","b","c","b")
year3<-c("a","b",NA,NA,NA,"c","b")
x<-data.frame(v1 =code.sandre,v2 =year1,v3 =year2, v4 =year3))
2/The dataframe I wanna get:
code_sandre<-c("A","B","C","D")
year1<-c("a","b",NA,"b")
year2<-c("a,b","b","c","b")
year3<-c("a,b",NA,"c","b")
result<-data.frame(code_sandre,year1,year2,year3)