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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T10:41:40+00:00 2026-06-14T10:41:40+00:00

I have a data frame, and make selections based on some of the factors.

  • 0

I have a data frame, and make selections based on some of the factors. I want a vector of names, created from the factor levels. Hopefully this suffices to show the intent:

test.results <- list(
  First  = factor(c('A', 'B'), levels=c('A', 'B', 'C')),
  Second = factor(c('E', 'F'), levels=c('E', 'F', 'G')),
  Third  = factor(c('X', 'Y'), levels=c('X', 'Y', 'Z'))
  )

# cols <-  c('First', 'Third'); TestName(test.results, cols) should return c('A X', 'B Y')

Here is an implementation. Is there a way to avoid the explicit ‘for’ loop?

TestName <- function(X, cols) {
  result <- character(length(cols))
  space <- '';
  for (i in cols) {
    result <- paste0(result, space, X[[i]]);
    space <- ' ';
  }
  return(result);
}
  • 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-14T10:41:41+00:00Added an answer on June 14, 2026 at 10:41 am

    Your data is not a data.frame in the example, but nevermind, the following will work regardless

    paste is vectorized, so as the question stands there should be no need for *apply or for loops

    testname <- function(x, .names){ do.call(paste, x[.names])}
    testname(test.results, c('First','Third'))
    ## [1] "A X" "B Y"
    

    You could add checks about whether x is a list and that names exist in x.

    EDIT — allowing sep to be set (or other variables) if you wished.

    testname <- function(x, .names,...){ do.call(paste, c(x[.names], list(...)))}
    testname(test.results, c('First','Third'), sep = '---')
    ## "A---X" "B---Y"
    

    If your data was a data.table then you could do the following

    library(data.table)
    DT <- as.data.table(test.results)
    
    DT[, paste(First, Third)]
    

    Or you could just stick with lists and data.frames and use with or evalq

    evalq(paste(First,Third), test.results)
    

    or

    with(test.results, paste(First, Third))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have data frame with some numerical variables and some categorical factor variables. The
I have a data frame and I want to make make subset( or select
I have a data frame that is some 35,000 rows, by 7 columns. it
I have a data frame consisting of results from multiple runs of an experiment,
I have a data frame with several columns; some numeric and some character. How
I have a data frame where the row.names correspond to date values. $ cat
I have the following data frame: id<-c(1,2,3,4) x<-c(0,2,1,0) A<-c(1,3,4,3) df<-data.frame(id,x,A) now I want to
If I have a data.frame containing the data I want to plot, what is
I have a data frame with three columns and I'd like to make a
I have a data frame with two columns, and want to create a third

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.