将一列表格除以另一列并插入新列

时间:2014-07-07 20:04:39

标签: r

以下是我输入的一个小例子:

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

我想计算每行从2010年秋季到2011年秋季的百分比变化,并将其放在表格的一列中。 对此有任何帮助将非常感激。

2 个答案:

答案 0 :(得分:5)

首先,没有理由在此处使用tables包。来自基地R的table(mydata$College, mydata$Term)会给你相同的结果。这两个选项的问题在于很难操纵它们的类。

更好的选择是使用包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 

答案 1 :(得分:0)

您也可以使用dplyr

require(dplyr)
require(tidyr)

mydata = tbl_df(mydata)

mydata %.%
  mutate (Term = make.names(Term)) %.%
  group_by (Term, College) %.%
  summarise (n=n()) %.%
  spread (Term, n, fill=0) %.%
  mutate (delta = (Fall.2011-Fall.2010) / Fall.2010)

#  College Fall.2010 Fall.2011     delta
#     COBA         3         5 0.6666667
#      COE         1         1 0.0000000
#    COLFA         0         2       Inf