2016-10-03 93 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) 
) 

它採用第一個線程作者條目(和他們的崗位),並把它放在上面的評論者(以及他們的意見)的頂部。