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)

3 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
  3. This is an awesome post.Really very informative and creative contents. These concept is a good way to enhance the knowledge.I like it and help me to development very well.Thank you for this brief explanation and very nice information.Well, got a good knowledge.

    R Programming Online Training|
    Hadoop Online Training
    Data Science Online Training|

    RispondiElimina