【问题标题】:How to describe dataframe category into section and content in r-markdown pdf?如何在 r-markdown pdf 中将数据框类别描述为部分和内容?
【发布时间】:2021-08-14 19:10:57
【问题描述】:
df

Id  Section       Comment
------------------------------------------------------------
1   Product A   this is the general comment for product A
2   Product A   this is the general comment for product A
3   Product A   this is the general comment for product A
4   Product B   this is the general comment for product B
5   Product B   this is the general comment for product B
8   Product C   this is the general comment for product C
9   Product C   this is the general comment for product C
10  Product C   this is the general comment for product C

以上是我的数据框。我正在寻找类似这样的输出

expected output:
------------------
Product A
this is the general comment for product A
this is the general comment for product A

Product B
this is the general comment for product B
this is the general comment for product B

如果我遍历每一行,我会以这种方式得到section and comment section and comment

我尝试使用基于节的索引,如下面的代码

我的代码

section_df<-df%>%select("Section")%>% dplyr::mutate(r_number = row_number()) %>%
group_by(`Section`) %>%
dplyr::summarise(index = min(r_number))

section_df<-section_df[order(section_df$index),]

for(i in 1:nrow(df)){
  if(section_df$index[i] == i){
    
    cat(df$Section[i])
    cat(df$Comment[i])

  }else{
    cat(df$Comment[i])
  }
  
}

【问题讨论】:

    标签: r latex r-markdown pdflatex


    【解决方案1】:
    section_df<-section_df[complete.cases(section_df), ]
    
    for(i in 1:nrow(df)){
      
      if(i %in% section_df$index){
        print(df$Section[i])
      }
      else{
        print(df$Comment[i])
      }
    
    }
    

    【讨论】:

    • 请不要发布仅代码的答案。给出一些上下文和解释,为什么这段代码解决了特定的问题。
    猜你喜欢
    • 1970-01-01
    • 2019-08-08
    • 1970-01-01
    • 1970-01-01
    • 2012-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-09-23
    相关资源
    最近更新 更多