[R] [FORGED] Re: R lappy, sapply or mapply question

liqunhan at yahoo.com liqunhan at yahoo.com
Sat Oct 10 18:17:17 CEST 2015


Dear Rolf and Adams,
Thanks for your help! works perfectly.though all columns are of the same type, for "safe", it is changed from data.frame to data.matrix as follows:
dmatrix <- data.matrix(dailyrecord[c("a", "e", "f")])
colnames(dmatrix) <- c("x5", "x6", "x7")
apply(dmatrix, 1, function(row) fun3(list1, list2, as.list(row)))
Thanks again!
Bests,LQ 
      From: Rolf Turner <r.turner at auckland.ac.nz>
 To: "Adams, Jean" <jvadams at usgs.gov>; liqunhan at yahoo.com 
Cc: "r-help at r-project.org" <r-help at r-project.org> 
 Sent: Friday, October 9, 2015 7:28 PM
 Subject: Re: [FORGED] Re: [R] R lappy, sapply or mapply question
   
On 10/10/15 10:56, Adams, Jean wrote:


> You were very close.  Try this.
>
> df <- data.frame(x5=dailyrecord$a, x6 = dailyrecord$e, x7 = dailyrecord$f)
> apply(df, 1, function(row) fun3(list1, list2, as.list(row)))


There could in general be problems with this approach.  The apply() 
function works on *matrices* and if handed a data frame coerces it to a 
matrix.  This is (usually!) OK if all columns of the data frame are of 
the same type, but the world could end if they are not. It is a good 
idea to *start* with a matrix if a matrix is what is required.  And do 
not confuse data frames with matrices.  They are very different animals.

cheers,

Rolf Turner

-- 
Technical Editor ANZJS
Department of Statistics
University of Auckland
Phone: +64-9-373-7599 ext. 88276


  
	[[alternative HTML version deleted]]



More information about the R-help mailing list