【问题标题】:How to create text from dataframe by two categorical columns如何通过两个分类列从数据框创建文本
【发布时间】:2019-02-02 02:00:13
【问题描述】:

我正在尝试获取一个过滤的文本值数据库表并创建一个文本报告,其中有可变数量的行(通过组合过滤表中的每一行的几列创建)以及创建的标题和子标题来自表中的其他 2 列。

这是发送到 rmarkdown 文档以创建 word 或 html 文档。我尝试转换为列表并使用 by() 和粘贴来组合列,但无法得到我想要的。

df=data.frame(cat1=c("A","A","A","B","B","C","C","C"),
  
  cat2=c("D","D","E","D","F","D","G","G"),
  
  text1=c("text1","text2","text3","text4","text5","text6","text7","text8"),
  
  text2=c("text9","text10","text11","text12","text13","text14","text15","text16"))

我想要类似的东西:

A:D:

文本1,文本9

文本2,文本10

甲:乙:

文本3,文本11

乙:丁:

文本4,文本12

B: F:

文本5,文本13

C:D:

文本6,文本14

C:G:

文本7,文本15

文本 8,文本 16

我得到了无法解析的复杂列表。

【问题讨论】:

    标签: r dataframe


    【解决方案1】:

    您可以通过 aggregatepaste 获得它

    T1 = aggregate(df$text1, list(df$cat1, df$cat2), paste, collapse = ", ")
    T2 = aggregate(df$text2, list(df$cat1, df$cat2), paste, collapse = ", ")
    T1$x = paste(T1$x, T2$x, sep=", ")
    T1
      Group.1 Group.2                            x
    1       A       D  text1, text2, text9, text10
    2       B       D                text4, text12
    3       C       D                text6, text14
    4       A       E                text3, text11
    5       B       F                text5, text13
    6       C       G text7, text8, text15, text16
    

    【讨论】:

    • 我的块引用发生了一些事情,并且行被连接起来。我已经编辑了我的原件以修复它,以免线条被折叠。我用df$line=paste(df$text1,df$text2,sep=", " 然后df_ag=aggregate(df$line, by=list(df$cat1,df$cat2), identity) 更接近我想要的。然后,我可以将两个组变量用于标题和用于在每个部分中打印的文本行号的索引。有更好的主意吗?
    • 知道为什么aggregate(..., identity) 命令会生成一个 x 列,其中包含来自原始数据帧而不是文本的行号索引? aggregate(..., print) 输组。
    【解决方案2】:

    这是我目前的解决方案:

    for (i in unique(df$cat1)) {
      cat(i,"\n")
      for (j in unique(df[df$cat1==i,"cat2"])) {
        cat(paste(" ",j),"\n")
        for (k in df$line[df$cat1==i & df$cat2==j])
        cat(paste("   ",k),"\n")
      }
    }
    

    这给出了:

    A 
      D 
        text1, text9 
        text2, text10 
      E 
        text3, text11 
    B 
      D 
        text4, text12 
      F 
        text5, text13 
    C 
      D 
        text6, text14 
      G 
        text7, text15 
        text8, text16 
    

    【讨论】:

      【解决方案3】:

      使用summarise 准备数据,然后使用cat 循环在rmarkdown 中打印。通过在块选项中使用results = "asis",您可以在cat 命令中包含格式。

      ```{r, results = "asis"}
      
      library(dplyr)
      
      newdf <- df %>% 
        group_by(cat1, cat2) %>%
        summarise(mystring = paste(text1, text2, sep = ", ", collapse = "  \n"))
      
      for (i in 1:nrow(newdf)) {
        with(newdf, cat("  \n####**", cat1[i], ": ", cat2[i], ":**", "  \n\n", mystring[i], "  \n", sep = "" )) }
      
      ```
      

      【讨论】:

      • 这与我想要的很接近,但是将 text1、text9 和 text2、text10 分成组,因此我可以将它们放在输出中的不同行上。我原来的帖子搞砸了。我的块引用发生了一些事情,并且行被连接起来。我已经编辑了我的原件以修复它,以便不会折叠行。
      猜你喜欢
      • 2019-12-20
      • 2018-01-07
      • 2019-05-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-02
      • 2016-05-22
      相关资源
      最近更新 更多