[R] new column that applies function to all rows based on last row

bcrombie bcrombie at utk.edu
Mon Mar 3 16:18:32 CET 2014


I know this is a simple question, but I am having trouble generating output
without errors.  I want to create a column containing values for each row
that are generated by dividing another column's row value by the same
column's sum (last-row value):

<http://r.789695.n4.nabble.com/file/n4686148/Capture.jpg> 

Here is the (less-than-elegant) practice script I'm working with (minus the
code generating errors):

mydata <- data.frame (CaseID =
c("1","2","3","4","5","6","7","8","9","10","11","12","13","14"),
          EstablishmentEEs = c(3,3,4,5,8,8,9,10,10,11,12,12,13,14))

freqdata.1n <- mydata$EstablishmentEEs
CHAR1n <- table(freqdata.1n)
CHAR1nmatrix <- as.data.frame(CHAR1n)
rownames(CHAR1nmatrix) <- CHAR1nmatrix$freqdata.1n
CHAR1n_total <- addmargins(as.table(as.matrix(CHAR1nmatrix[-1])), 1)
CHAR1n_total <- as.data.frame(CHAR1n_total)
CHAR1n_total <- subset(CHAR1n_total, select= -Var2)
library(gdata)
CHAR1n_total <-
rename.vars(CHAR1n_total,from="Var1",to="freqdata.1n",info=FALSE)

Thanks for your patience!



--
View this message in context: http://r.789695.n4.nabble.com/new-column-that-applies-function-to-all-rows-based-on-last-row-tp4686148.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list