- dcast:长数据变短数据,数据变形,汇总
长数据
name | department | title | salary |
---|---|---|---|
A | depart1 | title1 | 10000 |
B | depart2 | title1 | 12000 |
C | depart1 | title2 | 8000 |
D | depart3 | title2 | 7000 |
E | depart3 | title3 | 4000 |
F | depart2 | title3 | 3000 |
# 假设data.frame名称为mydata, 1式
dcast(mydata, name + department ~ title, sum, value.var = "salary")
# 注意value.var之后是跟一个带双引号的变量名称
# 还可以试一下, 2式
dcast(mydata, name ~ title + department, sum, value.var = "salary")
由1式,得到短数据:
name | department | title1 | title2 | title3 |
---|---|---|---|---|
A | depart1 | 10000 | 0 | 0 |
B | depart2 | 12000 | 0 | 0 |
C | depart1 | 0 | 8000 | 0 |
D | depart3 | 0 | 7000 | 0 |
E | depart3 | 0 | 0 | 4000 |
F | depart2 | 0 | 0 | 3000 |
- melt:短数据变长数据
短数据:
name | department | title1 | title2 | title3 |
---|---|---|---|---|
A | depart1 | 10000 | 0 | 0 |
B | depart2 | 12000 | 0 | 0 |
# 假设data.frame名称为mydata,式1
melt(mydata, id = c("name", "department"))
# 可以试一下,式2
melt(mydata, id = "name")
由式1,得到长数据:
name | department | variable | value |
---|---|---|---|
A | depart1 | title1 | 10000 |
B | depart2 | title1 | 12000 |
A | depart1 | title2 | 0 |
B | depart2 | title2 | 0 |
A | depart1 | title3 | 0 |
B | depart2 | title3 | 0 |