Re: [R] applying a function to data frame columns

From: Henrique Dallazuanna <wwwhsd_at_gmail.com>
Date: Fri, 22 Feb 2008 18:07:06 -0300

One thing is needed:

names(v) <- names(u)

PS:Thanks Mark Leeds

On 22/02/2008, Henrique Dallazuanna <wwwhsd_at_gmail.com> wrote:
> Try this:
>
> sapply(names(u), function(x)u[x][u[x] >=min(v[x]) & u[x] <= max(v[x])])
>
>
> On 21/02/2008, dxc13 <dxc13_at_health.state.ny.us> wrote:
> >
> > useR's,
> >
> > I want to apply this function to the columns of a data frame:
> >
> > u[u >= range(v)[1] & u <= range(v)[2]]
> >
> > where u is the n column data frame under consideration and v is a data frame
> > of values with the same number of columns as u. For example,
> > v1 <- c(1,2,3)
> > v2 <- c(3,4,5)
> > v3 <- c(2,3,4)
> > v <- as.data.frame(cbind(v1,v2,v3))
> >
> > uk1 <- seq(min(v1) - .5, max(v1) + .5, .5)
> > uk2 <- seq(min(v2) - .5, max(v2) + .5, .5)
> > uk3 <- seq(min(v3) - .5, max(v3) + .5, .5)
> >
> > u <- do.call("expand.grid", list(uk1,uk2,uk3))
> >
> > Here, there are 3 columns; instead of hard-coding this, can the function
> > given above, which will restrict the u data frame to values within the
> > ranges of each variable, be done with the apply function? Thanks in
> > advance.
> >
> > dxc13
> >
> > --
> > View this message in context: http://www.nabble.com/applying-a-function-to-data-frame-columns-tp15619657p15619657.html
> > Sent from the R help mailing list archive at Nabble.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.
> >
>
>
>
> --
> Henrique Dallazuanna
> Curitiba-Paraná-Brasil
> 25° 25' 40" S 49° 16' 22" O
>

-- 
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40" S 49° 16' 22" O

______________________________________________
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 Fri 22 Feb 2008 - 21:10:56 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 Fri 22 Feb 2008 - 22:30:15 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