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 6010395
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:06:36+00:00 2026-05-23T02:06:36+00:00

Is there a fast and clever way that would , lets say from DF

  • 0

Is there a fast and clever way that would , lets say from DF like this

vec <- data.frame(Names = c("var1","var2","var3","var4","var5","var6","var7",
                            "var8","var9","var10","var11","var12","var13",
                            "var14") ,
                  phase1= runif(14),
                  phase1.away= runif(14),
                  phase1_in= runif(14),
                  phase1_out= runif(14),
                  phase1.1= runif(14),
                  phase1.away.1= runif(14),
                  phase1_in.1= runif(14),
                  phase1_out.1= runif(14),
                  phase1.2= runif(14),
                  phase1.away.2= runif(14),
                  phase1_in.2= runif(14),
                  phase1_out.2= runif(14))

give a new DF as this:

-allways order according phase1.x , give the names of variables corresponding to the values, phase1_in and phase1_out values but not phase1.away.

What I am doing is simply

vec.o<-vec[with(vec, order(-phase1)),]
d1<-vec.o[c("Names","phase1","phase1_in","phase1_out")]

vec.o<-vec[with(vec, order(-phase1.1)),]
d2<-vec.o[c("Names","phase1.1","phase1_in.1","phase1_out.1")]

cbind(d1,d2)

which is extremely boring and I am also sure anti R-ish. Any clever ideas? I am dealing with large data frames permanently and R seems to be
a bit cumbersome. Is there any good literature one would reccomend for these purposes?
(load many variables, create names to them, operations with those variables etc…, )

  • 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-05-23T02:06:37+00:00Added an answer on May 23, 2026 at 2:06 am

    EDIT : corrected for the case phase.x goes to 10 and higher.

    I presume you have quite a lot more than phase1.1, phase1.2, so a general solution using regular expressions would be something along the lines of :

    # Make an id vector for the phase1.x, and give Names id -1
    # gives a warning as character is transformed to NA
    id <- as.numeric(gsub(".*\\.(\\d+$)","\\1",names(vec)))
    id[1] <- -1
    id[is.na(id)] <- 0 # first occurence, no .x
    
    
    veclist <- lapply(unique(id)[-1],function(i){
        #select all variables necessary, exclude the away
        out <- vec[id %in% c(i,-1) & !grepl("away",names(vec))]
        # find the phase1.x for ordering
        ovec <- grepl("phase1(\\.\\d+)?$",names(out))
        # order and produce
        out[order(out[,ovec]),]
    })
    
    do.call(cbind,veclist)
    

    It is based on recognition of the last number preceded by a dot, and cuts that out. If there is no last number preceded by a dot, it’s either the Names variable (which I indicate with -1), or the first phase (which I indicate with 0).

    Now you have an id vector that can easily select the variables that belong together, so you can loop over the unique values of id, except the first (being -1). Using regular expressions again you can get whatever variable you want for the construction of a new dataframe. The do.call on the end combines all those dataframes again.

    Btw, Ordering sub-dataframes goes quite a lot faster than ordering the original dataframe first and then selecting your variables. This is the gain you have in the solution of nullglob.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a reasonably fast way to extract the exponent and mantissa from a
Is there a fast way to do that?
Is there a fast way to move all the images and CSS from plug-ins'
Is there any fast algorithm that allows to compare two files (for verification purpose)
Is there a fast/efficiency way to check if a table is empty? DECLARE @StartEndTimes
It seems that there are several really fast prime factorization algorithms around (one that
I need to check in some fast way if there is any text nodes
Is there any fast way in C (below 1 sec) to find the number
Is there any fast way to verify null arguments via attributes or something? Convert
Is there any fast way to determine the size of the largest strongly connected

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.