[R] [Fwd: Re: How to apply functions over rows of multiple matrices]

2007-08-10 Thread Johannes Hüsing
[Apologies to Gabor, who I sent a personal copy of the reply
erroneously instead of posting to List directly]

[...]
  Perhaps what you really intend is to
 take the average over those elements in each row of the first matrix
which correspond to 1's in the second in the corresponding
 row of the second.  In that case its just:

 rowSums(newtest * goldstandard) / rowSums(goldstandard)


Thank you for clearing my thoughts about the particular example.
My question was a bit more general though, as I have different
functions which are applied row-wise to multiple matrices. An
example that sets all values of a row of matrix A to NA after the
first occurrence of TRUE in matrix B.

fillfrom - function(applvec, testvec=NULL) {
  if (is.null(testvec)) testvec - applvec
  if (length(testvec) != length(applvec)) {
stop(applvec and testvec have to be of same length!)
  } else if(any(testvec, na.rm=TRUE)) {
applvec[min(which(testvec)) : length(applvec)] - NA
  }
  applvec
}

fillafter - function(applvec, testvec=NULL) {
  if (is.null(testvec)) testvec - applvec
  fillfrom(applvec, c(FALSE, testvec[-length(testvec)]))
}

numtest - 6
numsubj - 20

newtest - array(rbinom(numtest*numsubj, 1, .5),
dim=c(numsubj, numtest))
goldstandard - array(rbinom(numtest*numsubj, 1, .5),
dim=c(numsubj, numtest))

newtest.NA - t(sapply(1:nrow(newtest), function(i) {
  fillafter(newtest[i,], goldstandard[i,]==1)}))

My general question is if R provides some syntactic sugar
for the awkward sapply(1:nrow(A)) expression. Maybe in this
case there is also a way to bypass the apply mechanism and
my way of thinking about the problem has to be adapted. But
as the *apply calls are galore in R, I feel this is a standard
way of dealing with vectors and matrices.





--

__
R-help@stat.math.ethz.ch 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.


Re: [R] [Fwd: Re: How to apply functions over rows of multiple matrices]

2007-08-10 Thread Johannes Hüsing
 1. matrices are stored columnwise so R is better at column-wise operations
 than row-wise.

I am seeing this by my code which contains more t() than
what seems healthy. However, the summaries are patient-wise
over repeated measurements. Out of convention, I am storing
patients in rows and measurements in columns.


 2. Here is one way to do it (although I am not sure its better than the
 index approach):

row.apply - function(f, a, b)
   t(mapply(f, as.data.frame(t(a)), as.data.frame(t(b


Ah, thank you so much. I'll take the generalization to N arguments
à la mapply() as an exercise for the reader.

 3. The code for the example in this post could be simplified to:

 first.1 - apply(cbind(goldstandard, 1), 1, which.max)
 ifelse(col(newtest)  first.1, NA, newtest)


Ouch! Consider this scholar slapped.

 4. given that both examples did not inherently need row by row operations
I wonder if that is the wrong generalization in the first place?


Given that you managed to squeeze my 20 lines of code into 2 lines
AND that row.apply() does not exist in base without many people
missing it, I'll have to concede this point and eliminate the
craving for row.apply() in favour of the whole-object approach.

__
R-help@stat.math.ethz.ch 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.


Re: [R] [Fwd: Re: How to apply functions over rows of multiple matrices]

2007-08-10 Thread Gabor Grothendieck
1. matrices are stored columnwise so R is better at column-wise operations
than row-wise.

2. Here is one way to do it (although I am not sure its better than the
index approach):

   row.apply - function(f, a, b)
  t(mapply(f, as.data.frame(t(a)), as.data.frame(t(b

3. The code for the example in this post could be simplified to:

first.1 - apply(cbind(goldstandard, 1), 1, which.max)
ifelse(col(newtest)  first.1, NA, newtest)

4. given that both examples did not inherently need row by row operations
   I wonder if that is the wrong generalization in the first place?


On 8/10/07, Johannes Hüsing [EMAIL PROTECTED] wrote:
 [Apologies to Gabor, who I sent a personal copy of the reply
 erroneously instead of posting to List directly]

 [...]
   Perhaps what you really intend is to
  take the average over those elements in each row of the first matrix
 which correspond to 1's in the second in the corresponding
  row of the second.  In that case its just:
 
  rowSums(newtest * goldstandard) / rowSums(goldstandard)
 

 Thank you for clearing my thoughts about the particular example.
 My question was a bit more general though, as I have different
 functions which are applied row-wise to multiple matrices. An
 example that sets all values of a row of matrix A to NA after the
 first occurrence of TRUE in matrix B.

 fillfrom - function(applvec, testvec=NULL) {
  if (is.null(testvec)) testvec - applvec
  if (length(testvec) != length(applvec)) {
stop(applvec and testvec have to be of same length!)
  } else if(any(testvec, na.rm=TRUE)) {
applvec[min(which(testvec)) : length(applvec)] - NA
  }
  applvec
 }

 fillafter - function(applvec, testvec=NULL) {
  if (is.null(testvec)) testvec - applvec
  fillfrom(applvec, c(FALSE, testvec[-length(testvec)]))
 }

 numtest - 6
 numsubj - 20

 newtest - array(rbinom(numtest*numsubj, 1, .5),
dim=c(numsubj, numtest))
 goldstandard - array(rbinom(numtest*numsubj, 1, .5),
dim=c(numsubj, numtest))

 newtest.NA - t(sapply(1:nrow(newtest), function(i) {
  fillafter(newtest[i,], goldstandard[i,]==1)}))

 My general question is if R provides some syntactic sugar
 for the awkward sapply(1:nrow(A)) expression. Maybe in this
 case there is also a way to bypass the apply mechanism and
 my way of thinking about the problem has to be adapted. But
 as the *apply calls are galore in R, I feel this is a standard
 way of dealing with vectors and matrices.





 --

 __
 R-help@stat.math.ethz.ch 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.


__
R-help@stat.math.ethz.ch 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.