我正在尝试获取一个过滤后的文本值数据库表,并创建一个文本报告,其中行数可变(通过将过滤后的表中的每一行合并几列来创建),并带有创建的标题和子标题从表中的其他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:
text1,text9
text2,text10
A:E:
text3,text11
B:D:
text4,text12
B:F:
text5,text13
C:D:
text6,text14
C:G:
text7,text15
text8,文本16
我得到了无法解析的复杂列表。
答案 0 :(得分:1)
您可以使用List<AuthorAndBooks> authorBooks = group.entrySet().stream().
map(entry->{
AuthorAndBooks ab = new AuthorAndBooks();
ab.setAuthor(entry.getKey());
ab.setAddedOn(entry.getValue().stream().findFirst().get().getAddedOn());
ab.setBooks(entry.getValue().stream().map(authorBook->{
AuthorBooks books = new AuthorBooks();
books.setName(authorBook.getBook().getName());
books.setAdded(authorBook.getAddedOn());
return books;
}).collect(Collectors.toList()));
return ab;
}).collect(Collectors.toList());
和aggregate
paste
答案 1 :(得分:0)
使用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 = "" )) }
```
答案 2 :(得分:0)
这是我当前的解决方案:
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