Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7822139
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T07:50:28+00:00 2026-06-02T07:50:28+00:00

I have the following code, z <- data.frame(a=sample.int(10),b=sample.int(10),c=sample.int(10)) letter <- c(a,c,b) # this will

  • 0

I have the following code,

z <- data.frame(a=sample.int(10),b=sample.int(10),c=sample.int(10))
letter <- c("a","c","b") # this will be used as the argument to a function
vec <- unlist(lapply(1:length(letter), 
              function(x) cat(paste("z[[letter[",x,"]]],",sep=""))))
vec[length(vec)] <- paste("z[[letter[",length(vec),"]]]",sep="")

Consequently:

> vec    
[1] "z[[letter[1]]]," "z[[letter[2]]]," "z[[letter[3]]]" 

I want to use vec to order the rows of dataframe z, using the code below,

z.sort <- z[with(z, order(???)),]

How can I get the character vector vec to be evaluated as the arguments to order?
Is there a better way of doing this bearing in mind that letter, which is used to form vec will be an argument to a function?

Desired output would be:

    a  b  c
5   1  1  9
10  2 10  2
1   3  7  1
9   4  2  5
8   5  8  6
2   6  4  3
4   7  9 10
3   8  3  8
6   9  5  7
7  10  6  4

or as dput output:

structure(list(a = 1:10, b = c(1L, 10L, 7L, 2L, 8L, 4L, 9L, 3L, 5L, 6L), c = c(9L, 2L, 1L, 5L, 6L, 3L, 10L, 8L, 7L, 4L)), .Names = c("a", "b", "c"), row.names = c(5L, 10L, 1L, 9L, 8L, 2L, 4L, 3L, 6L, 7L), class = "data.frame")
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T07:50:29+00:00Added an answer on June 2, 2026 at 7:50 am

    Here’s what you want (with different random data):

    > z[do.call(order, z[,letter]),]
        a  b  c
    5   1  2  1
    4   2  4  8
    1   3  3  9
    6   4  6  3
    8   5  8  5
    10  6  1  4
    2   7  5  7
    3   8 10  2
    9   9  7  6
    7  10  9 10
    

    do.call lets us send a list to a function as its arguments, so we can just reorder the columns of z and send them to order using do.call, as a data.frame is just a special kind of list.

    Works with no problem in a function:

    my.reorder <- function(dat, cols) { dat[do.call(order, dat[,cols]),] }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following dummy code: dt<-data.frame(country=letters[1:20],val=rnorm(20),siz=rnorm(20)) qplot(x=country,y=val,data=dt,geom=point,size=siz) Now I want to increase the
I have the following code function generate_pdf() { $fdf_data_strings = $this->get_hash_for_pdf(); #$fdf_data_names = array('49a'
I have following code to draw my logistic distribution: x=seq(-2000,2000,length=1000) dat <- data.frame(x=x) dat$value
I have the following code: Imports MySql.Data.MySqlClient Imports MySql.Data.Types Public Class FormMain Private Sub
I have the following test code use Data::Dumper; my $hash = { foo =>
I have the following postscript code: /outputtext { /data exch def /rot exch def
I have the following code I use to insert form data into a single
I have the following code foreach (DataRowView dr in Data) { if (dr ==
I have the following code: public void BeginConvert(object data) { ConverterData cObject = (ConverterData)data;
I have the following code: //-------------------------------------------------------------------------- // 2) Query database for data //-------------------------------------------------------------------------- $result

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.