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

2008-02-22 Thread Henrique Dallazuanna
One thing is needed:

names(v) <- names(u)



PS:Thanks Mark Leeds

On 22/02/2008, Henrique Dallazuanna <[EMAIL PROTECTED]> 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 <[EMAIL PROTECTED]> 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@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@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.


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

2008-02-22 Thread Tim Hesterberg
You can do:

lapply2(u, v, function(u,v) u[inRange(u, range(v))])

using two functions 'lapply2' and 'inRange' defined at bottom.
This basically does:

lapply(seq(along=u),
   function(i, U, V){
 u <- U[[i]]
 v <- V[[i]]
 u[u >= range(v)[1] & u <= range(v)[2]]
   },
   U = u, V = v)

Tim Hesterberg

>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

# inRange requires ifelse1, part of the "splus2R" package.

inRange <- function(x, a, b, strict = FALSE) {
  # Return TRUE where x is within the range of a to b.
  # If a is length 2 and b is missing, assume that a gives the range.
  # if(strict==FALSE), then allow equality, otherwise require a < x < b.
  # strict may be a vector of length 2, governing the two ends.
  if(length(a)==2) {
b <- a[2]
a <- a[1]
  }
  else if(length(a) * length(b) != 1)
stop("a and b must both have length 1, or a may have length 2")
  strict <- rep(strict, length=2)
  ifelse1(strict[1], x>a, x>=a) & ifelse1(strict[2], xhttps://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] applying a function to data frame columns

2008-02-22 Thread Henrique Dallazuanna
Try this:

sapply(names(u), function(x)u[x][u[x] >=min(v[x]) & u[x] <= max(v[x])])

On 21/02/2008, dxc13 <[EMAIL PROTECTED]> 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@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

__
R-help@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.


[R] applying a function to data frame columns

2008-02-21 Thread dxc13

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@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.