apply() in R with user-defined function
问题 I have a data frame with votes and party labels arranged thus dat <- data.frame( v1=c(25, 0, 70), v2=c(75, 100, 20), v3=c(0, 0, 10), l1=c("pA", ".", "pB"), l2=c("pB", "pC", "pC"), l3=c(".", ".", "pD") ) so that each row is a unit of analysis. Only vote-getting parties need consideration and this function extracts positive votes or the corresponding labels getpos <- function(vector, vorl="v"){ # change to "l" to report labels vot <- vector[grep( "v", colnames(vector) )]; lab <- vector[grep( "l