Thanks Petr, Actually, I figured it out last night. Please read on only if you're sort of interested.
You're absolutely right, I hastily changed my function to be more 'readable' without thinking that I was using a name that was already a function. What I wanted was a cross-tabulation of f1 and f2 as they appeared in the lengthy header names of my data. Using 'lapply' would definitely have saved me some trouble, but it didn't come to mind. It would mean I only had to use 1 loop instead of 2. Eventually, I built in an 'if' step to handle the case when there was no intersection for that given cell in the table and it made sure all the NA's in my data were set to zero. Thanks much for getting back, Petr. -Ben On Fri, Apr 15, 2011 at 3:18 AM, Petr PIKAL <petr.pi...@precheza.cz> wrote: > Hi > > r-help-boun...@r-project.org napsal dne 15.04.2011 01:34:22: > > > Hi everyone. > > > > I am quite frustrated that this doesn't work, as all the functions > within > > work fine by themselves. I'd also like any pointers to how to avoid > 'for' > > loops in my code. I understand it's less than desirable, but I'm still > quite > > new and use them a lot. > > > > I have a few wide datasets (90 to 120) with long column names, each name > > contains a number of different 'markers'. Each could be considered a > factor > > variable within the column name. Their are two categories of factors, > we'll > > call them f1 and f2. > > > > The data frame names look something like this: > > 'ace_van' , 'boy_van', 'car_xes' , 'ace_xes', 'dog_wall' , 'car_zounds' > > > > f1 <- c('ace', 'boy', 'car', 'dog') > > f2 <- c('van', 'wall', 'xes', 'zounds') # actual vectors are length 6 > and > > 7, so I don't want to individually sum the 42 combinations. > > > > > var.table <- function(data, vec1, vec2) > > { > > table <- as.data.frame(matrix(nrow = length(vec1), ncol = > length(vec2)), > > row.names = vec1) > > names(table) <- vec2 > > for (i in 1:length(vec1)) > > { > > for (j in 1:length(vec2)) > > { > > indices <- intersect(grep(vec1[j], names(data), value = TRUE), > > grep(cats[i], names(data), value = TRUE)) > > table[i,j] <- sum(data[ ,indices]) > > } > > } > > table > > } > > > > > var.table(mydf, f1, f2) > > > > Output: > > > > Error in FUN(X[[1L]], ...) : > > only defined on a data frame with all numeric variables > > > > > > Every entry in mydf is an integer with no missing values. > > I can not provide solution as I can not decipher what you want to do but > here are few comments. > > Do not use names of functions for naming objects e.g. table, it can be > quite confusing. > What is cats?? > Are you sure that mydf values are numbers? > > What does str(mydf) say? > > Do I understand that you want summarise all values in columns of data > frame that have some common name? > > > lapply(f2, grep, names(mydf)) > > will give you list of indices of columns in mydf matching particular f2 > item. From this you can continue. Maybe plyr package can also be used. > > Regards > > Petr > > > > > Thanks a ton. > > > > -Ben > > > > [[alternative HTML version deleted]] > > > > ______________________________________________ > > R-help@r-project.org mailing list > > https://stat.ethz.ch/mailman/listinfo/r-help > > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > > and provide commented, minimal, self-contained, reproducible code. > > [[alternative HTML version deleted]] ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.