2016-10-03 46 views
0

我有一个数据框可以提取发布在讨论论坛上的消息线索。通过从数据库连接表,我得到它看起来像这样的结构:R提取打印R标记的数据帧值

threadStarterName1 threadstarter1 comment1 commenterName1 
threadStarterName1 threadstarter1 comment2 commenterName2 
threadStarterName1 threadstarter1 comment3 commenterName3 
threadStarterName1 threadstarter1 comment4 commenterName4 
threadStarterName1 threadstarter1 comment5 commenterName5 

代码来创建这个数据帧:如下,

 df=data.frame("threadStarterName"=c("threadStarterName1","threadStarterName1","threadStarterName1","threadStarterName1","threadStarterName1"), 
"threadStarter"=c("threadStarter1","threadStarter1","threadStarter1","threadStarter1","threadStarter1"), 
"comment"=c("comment1","comment2","comment3","comment4","comment5"), 
"commenterName"=c("commenterName1","commenterName2","commenterName3","commenterName4","commenterName5")) 

我要重新格式化该数据帧中提取值,其然后我可以在R-markdown打印出报告:

threadstarter1 threadStarterName1 
    comment1  commenterName1 
    comment2  commenterName2 
    comment3  commenterName3 
    comment4  commenterName4 
    comment5  commenterName5 

在此先感谢!

+1

你可以发布你的代码到目前为止? – GrandMasterFlush

+1

http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example请从一个可重复的例子开始。 –

+0

在这篇文章中有些特定的东西不清楚(使用'dput()'或其他来自Brandon链接的建议可以修复):threadstarter和message1是同一列还是不同的列?是'row1 row2' ...'row.names'属性还是另一列?你的专栏是什么课?这是否需要推广到多个消息,还是数据框只包含'message1'?还有,你有什么尝试?它在哪里失败?你是怎么被卡住的? – Gregor

回答

0

如果我理解正确,原来的帖子帖子(及其作者)会在每一行上重复出现,而您希望它们只出现一次,并且与评论内容和评论作者位于同一列。

如果是这样,应该这样做:

onlyOnce <- 
    data.frame(
    user = c(df$threadStarterName[1] 
      , df$commenterName) 
    , commentPosted = c(df$threadStarter[1] 
         , df$comment) 
) 

它采用第一个线程作者条目(和他们的岗位),并把它放在上面的评论者(以及他们的意见)的顶部。