Melissa
Melissa

Reputation: 21

Calculating range for all variables

For someone new to R, what is the best way to view the range for a number of variables? I've run the summary command on the entire dataset, can I do range () on the entire dataset as well or do i need to create variables for each variable in the dataset?

Upvotes: 1

Views: 1405

Answers (1)

kangaroo_cliff
kangaroo_cliff

Reputation: 6222

For individual variable, you can use range. To see the range of multiple variables, you can combine range with one of the apply functions. See below for an example.

range(iris$Sepal.Length)
# [1] 4.3 7.9

sapply(iris[, 1:4], range)

#     Sepal.Length Sepal.Width Petal.Length Petal.Width
#[1,]          4.3         2.0          1.0         0.1
#[2,]          7.9         4.4          6.9         2.5

(only the first four columns were selected from iris since the 5th is a factor, and range doesn't apply for factors)

Upvotes: 1

Related Questions