Inner_join output not string as a column

Viewed 20

I want to join two data frames by the year date of the laydate df. I used the 'inner_join' function and it seemed to work. The data corresponds perfectly.

The problem is in the structure of the said data.

Here's the output:

tibble[,1] [31 × 1] (S3: tbl_df/tbl/data.frame)
 $ RCP.2.6.médiane: chr [1:31] "3,3" "3,9" "3,6" "3,7" ...

Eventually, I'll have to plot this data, and it's not accepted in this format. Is it possible to get it in simple character column like the other ones? Here are my two data sets:

kanasuta2:

structure(list(yearInt = 1950:2100, RCP.2.6.médiane = c("", 
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", 
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", 
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", 
"", "", "", "", "", "", "2,5", "2,9", "2", "3,3", "3,6", "3,3", 
"3,7", "3,6", "3,7", "4", "3,9", "2,8", "3,5", "3,5", "3,3", 
"4,5", "3,8", "4,2", "4", "3,9", "3,3", "4,2", "3,6", "3,3", 
"4,9", "3,2", "4,3", "3,5", "3,6", "4,5", "3,8", "4,1", "4,7", 
"3,8", "4,4", "3,9", "4,7", "4,5", "4", "4,2", "4,5", "4,3", 
"4,7", "4,7", "4,8", "4,7", "4,3", "4,3", "4,6", "4,4", "4,6", 
"4,1", "3,4", "4,1", "4,6", "3,9", "4", "4,8", "4,8", "3,7", 
"4,6", "4,1", "4,7", "4,9", "4,4", "3,6", "4,3", "4,1", "5", 
"4,6", "4,2", "4,1", "4,1", "4,6", "3,9", "4,8", "5,1", "4,9", 
"3,9", "3,9", "4,5", "3,6", "4,7", "4,6", "4,7", "4,9", "3,9", 
"3,6", "3,6", "4", "3,4", "4,4", "4,6", "4,3", "4,1", "3,4")), class = "data.frame", row.names = c(NA, 
-151L))

laydate

structure(list(anneeInt = c(2008L, 2015L, 2009L, 2013L, 2014L, 
2019L, 2012L, 2013L, 2015L, 2013L, 2009L, 2011L, 2013L, 2018L, 
2016L, 2008L, 2008L, 2013L, 2014L, 2013L, 2018L, 2014L, 2020L, 
2012L, 2020L, 2005L, 2020L, 2021L, 2021L, 2021L, 2021L), linked = structure(list(
    RCP.2.6.médiane = c("3,3", "3,9", "3,6", "3,7", "4", "3,3", 
    "3,6", "3,7", "3,9", "3,7", "3,6", "3,7", "3,7", "3,5", "2,8", 
    "3,3", "3,3", "3,7", "4", "3,7", "3,5", "4", "4,5", "3,6", 
    "4,5", "2,5", "4,5", "3,8", "3,8", "3,8", "3,8")), row.names = c(NA, 
-31L), class = c("tbl_df", "tbl", "data.frame"))), row.names = c(NA, 
-31L), class = c("tbl_df", "tbl", "data.frame"))

Here's my code for the 'inner_join' function:

laydate$linked<-laydate %>% 
        inner_join(kanasuta2, 
                   by = c(anneeInt = 'yearInt')) %>% 
        select(RCP.2.6.médiane)

Finally, there is not error message at the inner_join ouput. It's later on that I get this type of error message:

Error in hist.default(datesponte$linked) : 'x' must be numeric

If someone could help me with that, I'm really stuck there!

1 Answers

The issue is that you input your data as character so it can't be analyzed directly. You have to convert to numeric to make a histogram or perform any numeric operations. Also if you want to extract a column as a vector use dplyr::pull() rather than dplyr::select(). hist() expects a vector not a tibble.

Your data uses comma (,) as a decimal separator which happens to be different from my locale, so I specified that to be unambiguous. You may not require this depending on where your you system thinks it is.

library(tidyverse)

laydate %>% 
  inner_join(kanasuta2, by = c(anneeInt = 'yearInt')) %>% 
  pull(RCP.2.6.médiane) %>% 
  parse_number(locale = locale(decimal_mark = ",")) %>% 
  hist()

Created on 2022-07-18 by the reprex package (v2.0.1)

Related