apply() in R with user-defined function
apply, r
Solution
What's happening here is that the rows in the dataframe no longer have column names after being extracted by `apply` (but they do have `names`):
Try:
getpos <- function(x, vorl="v"){
vot <- x[grep( "v", names(x) )] ; lab <- x[grep( "l", names(x) )];
if (vorl=="v") {vot[vot>0]} else {lab[vot>0]};
}
> apply(dat, MARGIN=1, FUN=function(x2) getpos(x2, vorl="l") )
#-------------
[[1]]
l1
"pA"
[[2]]
l2
"pC"
[[3]]
l1 l3
"pB" "pD"
Problem
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", colnames(vector) )]; if (vorl=="v") {vot[vot>0]} else {lab[vot>0]}; } getpos(dat[1,]) # votes for obs 1 getpos(dat[1,], vorl="l") # labels for obs 1 ``` I wish to run function getpos in every row of data frame dat in order to produce lists with vote/label vectors of different length. Applying the function does not return what I expect: ``` apply(X=dat, MARGIN=1, FUN=getpos, vorl="l") ``` Can anyone spot the problem? And related, can this be achieved more efficiently?