You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在R中合并数据集后出现字符串值不完整问题

R合并数据集后字符串值不完整的解决方法

我在R中合并两个数据集时,遇到了合并后某变量字符串值不完整的问题——合并前目录数据中的字符串是完整的,但合并后部分字符串被截断。

我的R代码如下:

# Librerias
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
# 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)

解决步骤

  • 检查合并键的一致性
    确保两个数据集的Actividad列字符串完全匹配,包括首尾空格、特殊字符。可以用str_trim()去除多余空格:

    # 处理目录数据的合并键
    Catalogo_comercio_CIIU <- Catalogo_comercio_CIIU %>% 
      mutate(Actividad = str_trim(Actividad))
    # 处理待合并数据的合并键
    pre_merge_data <- rbind(CIIU_Importaciones, CIIU_Exportaciones) %>% 
      rename(Actividad="...1") %>% 
      mutate(Actividad = str_trim(Actividad))
    

    之后用处理后的pre_merge_data继续后续步骤再合并。

  • 调整Excel读取方式
    read_excel()可能对长字符串处理有限,尝试指定列类型为字符型,或换用openxlsx的read.xlsx():

    # 指定列类型读取
    Catalogo_comercio_CIIU <- read_excel(
      "Comercio/Catalogo comercio bienes CIIU.xlsx",
      col_types = rep("text", ncol(read_excel("Comercio/Catalogo comercio bienes CIIU.xlsx")))
    ) %>% mutate(ACTIV4=CIIU)
    
    # 或用openxlsx读取
    Catalogo_comercio_CIIU <- read.xlsx("Comercio/Catalogo comercio bienes CIIU.xlsx") %>% 
      mutate(ACTIV4=CIIU)
    
  • 替换合并函数
    改用dplyr的left_join()替代merge(),它对字符型变量的兼容性更好:

    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 ) %>% 
      left_join(Catalogo_comercio_CIIU, by="Actividad") %>% 
      select(-Grupo,-Detalle,-Categoria1,-Categoria2)
    
  • 检查字符串长度
    用str_length()对比合并前后的字符串长度,确认是否真的被截断:

    # 合并前目录数据的字符串长度
    Catalogo_comercio_CIIU %>% 
      mutate(str_len = str_length(Actividad)) %>% 
      select(Actividad, str_len)
    
    # 合并后数据的字符串长度
    Comercio_bienes_mensual %>% 
      mutate(str_len = str_length(Actividad)) %>% 
      select(Actividad, str_len)
    

内容的提问来源于stack exchange,提问作者Miguel A.

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.08 06:55:12