venerdì 3 agosto 2007

Sorting/ordering a data.frame according specific columns


x = rnorm(20)
y = sample(rep(1:2, each = 10))
z = sample(rep(1:4, 5))

data.df <- data.frame(values = x, labels.1 = y, labels.2 = z)
print(data.df)

# data ordered according to "labels.1" column
# and then "labels.2" column
nams <- c("labels.1", "labels.2")
data.df.sorted = data.df[do.call(order, data.df[nams]), ]
print(data.df.sorted)

2 commenti:

  1. I would like to konow how to determine the number of ecological type of species shared by sites in a data frame with variables consisting of species, type (H,F,T),and Sites (A,B,C). I have 75 species noting by presence absence on sites.

    Thank you.

    RispondiElimina
  2. To be capable to answer in some way to your question I need to take a look at your data.frame so, at least, post a chunk of your data.frame typing something like:
    head(my_data_frame)

    An advice: the stackoverflow web site is a good place to post your questions and to learn more about R and hundreds of other programming languages.

    RispondiElimina