[R] 使用R進行樞紐分析

分析時有時會用到樞紐分析,
R也有類似的功能,
就是plyr
如下面例子,
產生一個data frame,
其中包含組別、性別與年齡三個col,
我們可以使用ddply
依照組別與年齡作為分組標準,
計算各組的平均數與標準差。

另外也常遇到計算各組樣本出現次數,
那就可以用nrow來計算。
如果分組標準只有一個,
則可以使用最簡單的table。
下方有小範例。

In Excel, there is something called Pivot Table.
In R, you can use ddply to summarize data.
All you have to do is to give it a data frame,
set the columns which you want to operate,
and set what you want to do (such as sum, mean, counting frequency...),
and it will give you the result.

Apart from this,
if you want to count the frequency of the data,
you can just use table.
The relating example is down below.

沒有留言:

張貼留言