以下是我输入的一个小例子:
Term <- c("Fall 2010", "Fall 2010", "Fall 2011", "Fall 2011", "Fall 2011", "Fall 2011", "Fall 2010", "Fall 2010", "Fall 2011", "Fall 2011", "Fall 2011", "Fall 2011")
College <- c("COE", "COBA", "COBA", "COLFA", "COE", "COBA", "COBA", "COBA", "COBA", "COBA", "COBA", "COLFA")
mydata <- data.frame(Term, College)
mydata
#Used the tables library to create a count of the occurrences.
require(tables)
tab<- tabular(Factor(College) ~ (Factor(Term)), data=mydata)
tab
Run Code Online (Sandbox Code Playgroud)
我想计算每一行从2010年秋季到2011年秋季的百分比变化,并将其放在表格的一列中.任何有关这方面的帮助将非常感激.
首先,没有理由在tables这里使用包.table(mydata$College, mydata$Term)从基地R将给你相同的结果.这两个选项的问题在于很难操纵它们的类.
更好的选择是使用dcast包reshape2
library(reshape2)
tab <- dcast(mydata, College ~ Term)
tab$Per_Change <- tab[, 3]/tab[, 2]
tab
## College Fall 2010 Fall 2011 Per_Change
## 1 COBA 3 5 1.666667
## 2 COE 1 1 1.000000
## 3 COLFA 0 2 Inf
Run Code Online (Sandbox Code Playgroud)