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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T22:57:31+00:00 2026-06-10T22:57:31+00:00

I have an example data frame as shown below. > x=data.frame(id=1:5,c1=letters[1:5],c2=letters[13:17]) > x id

  • 0

I have an example data frame as shown below.

> x=data.frame(id=1:5,c1=letters[1:5],c2=letters[13:17])
> x
  id c1 c2
1  1  a  m
2  2  b  n
3  3  c  o
4  4  d  p
5  5  e  q

I want to create a vector out of this data frame which selects a different column for each row based on another vector. So if that vector is

> vars
[1] 1 2 2 1 1 
> 

I want for the 1st row in x, column 1, for the second row in x, column 2 and so on. So the expected output vector (or data frame) would be
if vector

a n o d e

if data frame

id V1
 1  a
 2  n
 3  o
 4  d
 5  e

Any help, much appreciated.

  • 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-10T22:57:33+00:00Added an answer on June 10, 2026 at 10:57 pm

    You can ‘slice’ a data frame using a matrix:

    y=data.frame(1:5,c(1,2,2,1,1))
    x[2:3][as.matrix(y)]
    

    result:

    [1] "a" "n" "o" "d" "e"
    

    Let’s generalise this by creating a function

    selector=function(x)matrix(c(seq_along(x),x),ncol=2)
    

    Note that there is one column to be ignored at the start, so add 1 to your select vector v

    v=c(1,2,2,1,1)
    x[selector(v+1)]
    

    result

    [1] "a" "n" "o" "d" "e"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a list within a data frame (in this example, df$candpct ), and
I have a 3 column data frame which looks a little like this: id
I have a data.frame in R which has been constructed off the Example 1-3
I have many data.frames, for example: df1 = data.frame(names=c('a','b','c','c','d'),data1=c(1,2,3,4,5)) df2 = data.frame(names=c('a','e','e','c','c','d'),data2=c(1,2,3,4,5,6)) df3 =
I have some example target = $this.attr('data-target') || e.preventDefault() || (href = $this.attr('href')) &&
I have a file containing some data (for example, 00927E2B112DB958......). This data is a
I have a large data frame with the following fields (example data). #dput(data) gives...
I have a data set which is similar to df <- data.frame(cbind( c(rep.int(x =
Let's say I have a data frame, like this: df <- data.frame( variable =
I'm trying to normalize some data which I have in a data frame. I

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.