Re: [R] apply min function rowwise

From: moleps <moleps2_at_gmail.com>
Date: Sat, 05 Jun 2010 20:14:13 +0200

Appreciate it...

//M

On 5. juni 2010, at 20.11, Joshua Wiley wrote:

> On Sat, Jun 5, 2010 at 10:22 AM, moleps <moleps2_at_gmail.com> wrote:
>> thx.
>> 
>> It was only the first instance that was class date. The rest were factors. So that explains it.
>> 
>> If I want to change the rest in vec into class date (there are many of them...)
>> 
>> neither  as.Date(canc[,vec],"%d.%m.%Y") nor sapply(canc[,vec],FUN=function(x) as.date(x,"%d.%m.%Y")) works
>> 
>> What is the easy solution to this?
> 
> This is the nicest solution that comes to mind:
> 
> as.data.frame(lapply(X=samp.dat, FUN=as.Date, format="%d.%m.%Y"))
> 
> I believe the problem is that sapply() coerces the results (by default
> when simplify=TRUE) using as.vector() leaving you with the number of
> days since the origin.  Anyway, using as.data.frame() on the list
> output from lapply() seems to work.
> 
> Josh
> 
>> 
>> Regards,
>> 
>> //M
>> 
>> 
>> 
>> On 5. juni 2010, at 18.30, Joshua Wiley wrote:
>> 
>>> Hello M,
>>> 
>>> My guess is that it has something to do with the class of the
>>> variables.  Perhaps you could provide a small sample dataframe?  Also
>>> you might try running str() on your data frame and seeing if the
>>> results are what you would expect.  As a side note, it is not
>>> necessary to make an anonymous function here, as you are allowed to
>>> pass arguments to the function applied.
>>> 
>>> apply(canc[,vec],1, min, na.rm=TRUE)
>>> 
>>> Best regards,
>>> 
>>> Josh
>>> 
>>> On Sat, Jun 5, 2010 at 8:30 AM, moleps <moleps2_at_gmail.com> wrote:

>>>> Im trying to tease out the minimum value from a row in a dataframe where all the variables are dates.
>>>>
>>>> apply(canc[,vec],1,function(x)min(x,na.rm=T))
>>>>
>>>>
>>>> However it only returns empty strings for the entire dataframe except for one date value (which is not the minimum date).
>>>>
>>>> Ive also tried
>>>>
>>>> apply(canc[,vec],1,function(x)max(x,na.rm=T))
>>>>
>>>> which provides values rowwise, but many of them are not in fact the largest in the row.
>>>>
>>>>
>>>> Any advice?
>>>>
>>>>
>>>> Regards,
>>>>
>>>> //M
>>>>
>>>> ______________________________________________
>>>> R-help_at_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.
>>>>
>>> 
>>> 
>>> 
>>> --
>>> Joshua Wiley
>>> Senior in Psychology
>>> University of California, Riverside
>>> http://www.joshuawiley.com/
>> 
>> 
> 
> 
> 
> -- 
> Joshua Wiley
> Senior in Psychology
> University of California, Riverside
> http://www.joshuawiley.com/

______________________________________________
R-help_at_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. Received on Sat 05 Jun 2010 - 21:40:16 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Sat 05 Jun 2010 - 22:10:29 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive