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

R语言如何合并向量内元素 将pdftools提取的多行PDF表转为数据框

处理方案

首先观察清洗后的scale_definitions向量结构,每4个元素对应原始表格的一行数据,按这个规律拆分合并即可得到规范数据框,完整代码如下:

首先加载依赖包(已安装可跳过安装步骤):

install.packages("tidyverse")
library(tidyverse)

你的原始数据处理部分:

scale_definitions <- c("", "                                        to lack passion                        easily annoyed", 
                       "      Excitable", "                                        to lack a sense of urgency             emotionally volatile", 
                       "", "                                        naive                                  mistrustful", 
                       "      Skeptical", "                                        gullible                               cynical", 
                       "", "                                        overly confident                       too conservative", 
                       "      Cautious", "                                        to make risky decisions                risk averse", 
                       "", "                                        to avoid conflict                      aloof and remote", 
                       "      Reserved", "                                        too sensitive                          indifferent to others' feelings", 
                       "", "                                        unengaged                              uncooperative", 
                       "      Leisurely", "                                        self-absorbed                          stubborn", 
                       "", "                                        unduly modest                          arrogant", 
                       "      Bold", "                                        self-doubting                          entitled and self-promoting", 
                       "", "                                        over controlled                        charming and fun", 
                       "      Mischievous", "                                        inflexible                             careless about commitments", 
                       "", "                                        repressed                              dramatic", 
                       "      Colorful", "                                        apathetic                              noisy", 
                       "", "                                        too tactical                           impractical", 
                       "      Imaginative", "                                        to lack vision                         eccentric", 
                       "", "                                        careless about details                 perfectionistic", 
                       "      Diligent", "                                        easily distracted                      micromanaging", 
                       "", "                                        possibly insubordinate                 respectful and deferential", 
                       "      Dutiful", "                                        too independent                        eager to please"
)

# 替换连续多空格为|
scale_definitions <-  scale_definitions %>% str_replace_all("\\s{2,}", "|")

核心整理代码:

df <- tibble(raw = scale_definitions) %>%
  # 每4个元素分为一组,对应原始表格一行
  mutate(group_id = rep(1:(n()/4), each = 4)) %>%
  group_by(group_id) %>%
  summarise(
    # 提取量表名称
    scale_name = str_split(raw[3], "\\|", simplify = T)[,2],
    # 合并低得分维度的两行描述
    low_feature = paste(str_split(raw[2], "\\|", simplify = T)[,2], str_split(raw[4], "\\|", simplify = T)[,2]),
    # 合并高得分维度的两行描述
    high_feature = paste(str_split(raw[2], "\\|", simplify = T)[,3], str_split(raw[4], "\\|", simplify = T)[,3])
  ) %>%
  ungroup() %>%
  select(-group_id)

运行后得到的df为三列标准数据框,和原始表格结构完全匹配。

内容的提问来源于stack exchange,提问作者user1828605

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.06 06:12:02