[R] Questions on dividing lists and tapply

From: Rohit Pandey <rohitpandey576_at_gmail.com>
Date: Wed, 16 Mar 2011 02:36:52 +0530

I have two questions about using R.

The first is about dividing each element of a list with another similar sized list. So, if the first list has two elements and so does the second, then the result should also be a list with two elements.

For example, the inputs are:

list(matrix(1:6,ncol=2),matrix(1:6,ncol=2))->l1 l2<-list(1:3,2)

I want to get a list, l3 with the first element being l1[[1]]/l2[[1]] and the second one, l1[[2]]/l2[[2]].

I had asked a similar question before and the solution using mapply works well for a list and a vector, but for two lists, it will return an array (and not a list).

My second question is about the tapply function.

tapply(1:5,list(c(1,1,2,2,3),c(1,2,1,1,3)),sum) will give me:

1 2 3
1 1 2 NA
2 7 NA NA
3 NA NA 5
The index here is a list: list(c(1,1,2,2,3),c(1,2,1,1,3)).

However, if I get the same index list through one of the other apply functions (like by) for example,
indxLi<-by(rbind(c(1,1,2,2,3),c(1,2,1,1,3)),1:2,function(x){return(x)}) then the tapply no longer works with this list. tapply(1:5,indxLi,sum) gives me the error:

Error in sort.list(y) : 'x' must be atomic for 'sort.list' Have you called 'sort' on a list?

Is there a way to use the above list in the tapply function?

```--
Thanks,
Rohit
Mob: 91 9819926213

[[alternative HTML version deleted]]

______________________________________________
R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help