2012-07-04 26 views
0

我有一項調查的數據。我有來自幾個李克特型問題的數據。例如:「下列因素在多大程度上影響您決定參加博士課程?」有4個類別:1:根本沒有; 2:在很小程度上; 3:在一定程度上; 4:在相當大的程度上)如何在R中列表評分或分類數據?

我導出的數據,它目前看起來是這樣的:

id Q1_Item1 Q1_Item2 Q1_Item3 Q1_Item4 
1   4   4   4   2 
2   1   2   3   4 
3   3   4   4   4 
4   3   3   3   3 
5   2   1   1   1 

我想製表的數據,因此它看起來像這樣

 Not at all To a small extent To some extent To a great extent 
Item1   1     1    2     1 
Item2   1     1    1     2 
Item3   1     0    2     2 
Item4   1     1    1     2 

其中數字現在代表每個類別下的迴應計數。我如何在R中做到這一點?

+0

好了,你有什麼問題嗎?你有什麼嘗試?你卡在哪裏? – Andrie

+1

看看'help(table)'。 – Benjamin

+0

Andrie,這實際上是我在Stackoverflow上的第一個問題。我試圖弄清楚如何格式化數據在這裏發佈。我現在意識到我在完成之前發佈了內容,但現在我已經對其進行了編輯。歡呼聲, –

回答

4

假設這些都被讀入作爲與文本輸入因素,如標籤,那麼這個工程:

# If the data was read in as numeric, then this will convert to factor-class 
dfrm[-1] <- lapply(dfrm[-1], factor, levels=1:4, labels=c("Not at all", 
         "To a small extent", "To some extent", "To a great extent")) 
t(sapply(dfrm[-1], table)) 
     Not at all To a small extent To some extent To a great extent 
factor1   2     1    4     3 
factor2   0     0    3     8 
factor3   0     0    3     9 
factor4   0     0    6     6 
factor5   0     0    3     8 
factor6   2     0    0    10 
factor7   0     0    2    10 
+0

DWin,你知道是否可以使用xtable或其他包導出表嗎?我試圖用xtable來做到這一點,但沒有運氣。 –

+0

我不是可以運行R的電腦,所以我無法測試我的想法。該函數的輸出是一個矩陣,所以任何處理矩陣的輸出都可以工作。同樣的,如果你有一個可以處理data.frame的方法,你可以把它包裝在'as.data.frame'中。 –