我有以下数据框;
Fruit <- c("orange", "orange", "apple", "pineapple", "lemon", "apple", "orange")
Name <- c("julius", "julius", "john", "mary", "kathy", "john", "julius")
df <- data.frame(Fruit, Name);df
我的目标是得到每个人吃掉的所有水果的总和,以便最后得到下表;
orange apple pineapple lemon
julius 2 1
john 2
mary 1
kathy 1 1
我正在尝试聚合功能,但只能设法输出每个人吃掉的水果总数,如下所示;
df2 <- aggregate(Fruit~Name,df,length); df2
输出是;
Name Fruit
1 john 2
2 julius 3
3 kathy 1
4 mary 1
任何帮助将不胜感激。感谢
答案 0 :(得分:4)
选项1
library(reshape2)
dcast(df, Name~Fruit)
Name apple lemon orange pineapple
1 john 2 0 0 0
2 julius 0 0 3 0
3 kathy 0 1 0 0
4 mary 0 0 0 1
选项2
table(df)
# as pointed out by lebatsnok, the general command would be with(df, table(Fruit, Name))
Name
Fruit john julius kathy mary
apple 2 0 0 0
lemon 0 0 1 0
orange 0 3 0 0
pineapple 0 0 0 1
答案 1 :(得分:2)
看起来你想要一个简单的双向频率表:
table(Fruit, Name)
# Name
#Fruit john julius kathy mary
# apple 2 0 0 0
# lemon 0 0 1 0
# orange 0 3 0 0
# pineapple 0 0 0 1
答案 2 :(得分:1)
> library(gmodels)
>
> CrossTable(Fruit, Name)
Cell Contents
|-------------------------|
| N |
| Chi-square contribution |
| N / Row Total |
| N / Col Total |
| N / Table Total |
|-------------------------|
Total Observations in Table: 7
| Name
Fruit | john | julius | kathy | mary | Row Total |
-------------|-----------|-----------|-----------|-----------|-----------|
apple | 2 | 0 | 0 | 0 | 2 |
| 3.571 | 0.857 | 0.286 | 0.286 | |
| 1.000 | 0.000 | 0.000 | 0.000 | 0.286 |
| 1.000 | 0.000 | 0.000 | 0.000 | |
| 0.286 | 0.000 | 0.000 | 0.000 | |
-------------|-----------|-----------|-----------|-----------|-----------|
lemon | 0 | 0 | 1 | 0 | 1 |
| 0.286 | 0.429 | 5.143 | 0.143 | |
| 0.000 | 0.000 | 1.000 | 0.000 | 0.143 |
| 0.000 | 0.000 | 1.000 | 0.000 | |
| 0.000 | 0.000 | 0.143 | 0.000 | |
-------------|-----------|-----------|-----------|-----------|-----------|
orange | 0 | 3 | 0 | 0 | 3 |
| 0.857 | 2.286 | 0.429 | 0.429 | |
| 0.000 | 1.000 | 0.000 | 0.000 | 0.429 |
| 0.000 | 1.000 | 0.000 | 0.000 | |
| 0.000 | 0.429 | 0.000 | 0.000 | |
-------------|-----------|-----------|-----------|-----------|-----------|
pineapple | 0 | 0 | 0 | 1 | 1 |
| 0.286 | 0.429 | 0.143 | 5.143 | |
| 0.000 | 0.000 | 0.000 | 1.000 | 0.143 |
| 0.000 | 0.000 | 0.000 | 1.000 | |
| 0.000 | 0.000 | 0.000 | 0.143 | |
-------------|-----------|-----------|-----------|-----------|-----------|
Column Total | 2 | 3 | 1 | 1 | 7 |
| 0.286 | 0.429 | 0.143 | 0.143 | |
-------------|-----------|-----------|-----------|-----------|-----------|