0

I wanted to merge two datasets and the merging resulted in getting incomplete strings in a variable

# Librerias
```{r}
library(dplyr)
library(tidyr)
library(tidyverse)
library(readxl)
library(readr)
library(base)
library(stringr)
library(foreign)
library(forcats)
library(fs)
library(hablar)
library(openxlsx)
```
# Comercio CIIU
```{r}
# Mercancias
Catalogo_comercio_CIIU <- read_excel("Comercio/Catalogo comercio bienes CIIU.xlsx") %>%
  mutate(ACTIV4=CIIU)

CIIU_Exportaciones <- read_excel("Comercio/CIIU Exportaciones.xlsx") %>% 
  pivot_longer(cols = -...1, names_to = "Period", values_to = "Valor") %>% 
  mutate(Flujo="Exportaciones")

CIIU_Importaciones <- read_excel("Comercio/CIIU Importaciones.xlsx") %>% 
  pivot_longer(cols = -...1, names_to = "Period", values_to = "Valor") %>% 
  mutate(Flujo="Importaciones")

Length<- CIIU_Exportaciones %>% group_by(...1) %>% 
  summarise(Obs=n())
Length<- Length$Obs[[1]] %>% 
  as.numeric() %>% 
  as.vector()

# Mensual
Comercio_bienes_mensual <- rbind(CIIU_Importaciones, CIIU_Exportaciones) %>% 
  rename(Actividad="...1") %>% group_by(Flujo, Actividad) %>% 
  mutate(Fecha=seq(from=as.Date("1994-01-01"), by="month", length.out=Length)) %>% 
  mutate(Year=str_sub(Fecha, 1L,4L), Mes=str_sub(Fecha, 6L,7L)) %>% 
  group_by(Flujo, Actividad, Year) %>% 
  mutate(Acumulado=cumsum(Valor)) %>% 
  group_by(Flujo, Actividad) %>% 
  mutate( C_acumulado= Acumulado-lag(Acumulado, n=12L), TC_acumulado=Acumulado/lag(Acumulado, n=12L)-1 ) %>% 
  merge(Catalogo_comercio_CIIU,  by="Actividad") %>% 
  select(-Grupo,-Detalle,-Categoria1,-Categoria2)
```

This is the catalogue I want to merge

This is the catalogue I want to merge

This is how the merging turned out (incomplete string)

This is how the merging turned out (incomplete string)

I am a beginner so I don't really know what to try

r2evans
  • 141,215
  • 6
  • 77
  • 149
  • 5
    Welcome to SO, Miguel A.! Please do not post (only) an image of code/data/errors: it breaks screen-readers and it cannot be copied or searched (ref: https://meta.stackoverflow.com/a/285557 and https://xkcd.com/2116/). Please include the code, console output, or data (e.g., `data.frame(...)` or the output from `dput(head(x))`) directly. – r2evans Dec 15 '22 at 21:10
  • 1
    Side note: you're already using `dplyr`, I suggest you shift from using `base::merge` and look at `dplyr::inner_join` (if that's what you want) or one of the other `dplyr::*_join` variants. If you aren't certain about the differences between left/right/full/inner joins, see https://stackoverflow.com/q/1299871/3358272, https://stackoverflow.com/q/5706437/3358272 – r2evans Dec 15 '22 at 21:11
  • 1
    Thabk you! It was solved – Miguel A. Dec 16 '22 at 19:07

0 Answers0