如何解决如何将数据框类别描述为 r-markdown pdf 中的部分和内容?
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])
}
}
解决方法
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])
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。