R_User
R_User

Reputation: 11082

How to determine the overall range of several variables of a data.frame, using variable indices?

I have a data.frame that looks similar to:

data <- data.frame(
  x=c(1:10)
  , y1=c(3,5,1,8,2,4,5,0,8,2)
  , y2=c(1,8,2,1,4,5,3,0,8,2)
  , y3=c(13,88,2,1,4,5,3,0,-8,1)
)

(Actually there are much more variabes in the data.frame and the variables have longer names.)

I'd like to determine the range of

  1. the variable x
  2. of all the other variables

one way could be:

xRange = range(data$x)

# using variable names:
yRange = range(data$y1, data$y2, data$y3)

# using variable indices
yRange = range(data[[2]], data[[3]], data[[4]])

I also tried to use ranges, so that I dont habe to add every single variable name or index:

yRange = range(data[[c(2:4)]])
yRange = range(data[[2:4]])

Is there a way to get a list of all values from sveral variables of a data.frame?

Upvotes: 0

Views: 647

Answers (1)

agstudy
agstudy

Reputation: 121578

It is not clear what you try to do. But why not to use subset to remove some variable by name and get range of the result?

range(subset(data,select=-x))
-8 88

Or equivalent to this by using variable indexes ( not recommended)

range(subset(data,select=2:4))

Upvotes: 1

Related Questions