Associate trees with a year to the associated year temperature in the other data set.

Macintosh; Intel Mac OS X 12_4_0
I have two data sets. One named 'datesponte' and the other 'kanasuta'. In 'datesponte' I have trees representing a bird nesting site. A column of years ($annee) associated to each tree are in 'character format'.

I want to associate these trees to the correct year temperature ($Historique.modélisé) in the data set 'kanasuta'.

Structure of the df 'datesponte':

structure(list(CodeNidif = c("A08B08_200801", "A08E31_201501",
"A09A03_200901", "A12B01_201301", "A12B01_201401", "B12RJ52_201201"
), NumCav = c(1, 2, 1, 1, 1, 1), NbVisites = c(8, 4, 2, 1, 1,
4), DatePonte = structure(c(1212105600, 1431388800, 1242086400,
1368316800, NA, 1336867200), tzone = "UTC", class = c("POSIXct",
"POSIXt")), CI(jours) = c(1, 1, 2, 2, NA, 1), Succes = c("N",
"P", "NA", "NA", NA, "P"), NbJeunesSucces = c("NA", "2", "NA",
"NA", NA, "3"), annee = c("2008", "2015", "2009", "2013", NA,
"2012")), row.names = c(NA, -6L), class = c("tbl_df", "tbl",
"data.frame"))

Structure of df 'kanasuta':

structure(list(DateTime = c("1950-05-01", "1951-05-01", "1952-05-01",
"1953-05-01", "1954-05-01", "1955-05-01"), Historique.modélisé = c("2,3",
"2,4", "2,5", "2,2", "2,3", "2,1"), Répartition.historique..low. = c("0,4",
"0,4", "0,5", "0,5", "-0,3", "0,2"), Répartition.historique..high. = c("4,3",
"4,8", "4,7", "3,8", "4", "4,7"), RCP.2.6.médiane = c("", "",
"", "", "", ""), RCP.2.6.portée..low. = c("", "", "", "", "",
""), RCP.2.6.portée..high. = c("", "", "", "", "", ""), RCP.4.5.médiane = c("",
"", "", "", "", ""), RCP.4.5.portée..low. = c("", "", "", "",
"", ""), RCP.4.5.portée..high. = c("", "", "", "", "", ""),
RCP.8.5.médiane = c("", "", "", "", "", ""), RCP.8.5.portée..low. = c("",
"", "", "", "", ""), RCP.8.5.portée..high. = c("", "", "",
"", "", ""), year = c("1950", "1951", "1952", "1953", "1954",
"1955")), row.names = c(NA, 6L), class = "data.frame")
I tried running the following code:

essai=numeric(37)

for(i in 1:37) {

essai[i]=kanasuta$Historique.modélisé[kanasuta$year==datesponte$annee[1]]

}

kanasuta$Historique.modélisé[kanasuta$year==datesponte$annee[1]] [1] ""

It doesn't seem to work. I'm a beginner in R, and I could really use someone's help. Thanks!

Hi!

To help us help you, could you please prepare a reproducible example (reprex) illustrating your issue? Please have a look at this guide, to see how to create one:

Ok I used the dput() function. Is it better that way ?

Not really, you haven't posted the complete dput() output so we can't use it.

Ok, yeah I cut somes parts out that weren't necessary. I added the full output now. Is it ok ?
Thanks for the follow up btw !

I think you can get what you want by using dplyr::inner_join() but there are no matching years in your sample data so I can't test it with the data you are providing. The code would be something like this

library(dplyr)

datesponte %>% 
    inner_join(kanasuta, by = c(annee = 'year')) %>% 
    select(Historique.modélisé)

Hi, yes that worked ! Thank you so much!
I appreciate it.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.