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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T15:38:24+00:00 2026-06-03T15:38:24+00:00

There is a data file with ID and columns y1, y2, … > dat

  • 0

There is a data file with ID and columns y1, y2, …

> dat
   id y1 y2 y3 y4 y5
1   1  2  4  2  3  4
2   2  4  1  1  4  4
3   3  2  3  1  3  1
4   4  0  1  2  4  4
5   5  3  3  2  1  4
6   6  4  1  3  1  4
7   7  2  4  4  4  0
8   8  2  5  0  2  5
9   9  2  2  3  3  1
10 10  0  3  4  4  3

There is a sample file with subset of IDs and week numbers.

> sam
  id week
1  2    3
2  4    4
3  6    4
4  7    5
5  8    5

I want to assign a y value from dat to each sampled ID according the week number. This is the result I would like to achieve:

> res
  id y
1  2 1
2  4 4
3  6 1
4  7 0
5  8 5

It is possible to achieve the result with some looping but I would like to avoid it if possible.

The structure of dat and sam.

dat <- structure(list(id = 1:10,
    y1 = c(2, 4, 2, 0, 3, 4, 2, 2, 2, 0),
    y2 = c(4, 1, 3, 1, 3, 1, 4, 5, 2, 3),
    y3 = c(2, 1, 1, 2, 2, 3, 4, 0, 3, 4),
    y4 = c(3, 4, 3, 4, 1, 1, 4, 2, 3, 4),
    y5 = c(4, 4, 1, 4, 4, 4, 0, 5, 1, 3)),
  .Names = c("id", "y1", "y2", "y3", "y4", "y5"),
  row.names = c(NA, -10L),
  class = "data.frame")

sam <- structure(list(id = c(2L, 4L, 6L, 7L, 8L),
    week = c(3L, 4L, 4L, 5L, 5L)),
  .Names = c("id", "week"),
  row.names = c(NA, -5L),
  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-03T15:38:26+00:00Added an answer on June 3, 2026 at 3:38 pm
    data.frame(id = sam[,1], y = dat[cbind(sam[,1],(1+sam[,2]))])
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I have a data file with columns of numbers like 3.14 0.42 6.66
I am reading in data from a file that has three columns. For example
Is there a platform-independent method to embed file data into a c++ program? For
Is there a way to import data from a JSON file into R? More
There's a loop where we get certain data. Depending on the data, file writer
I want to import data from an Excel file - assume Excel 2003 /
i have a data file(csv) consisting of 2 columns & 1000 rows, as i
I have a CSV data file with rows that may have lots of columns
I have a text file containing 10 columns of numbers. What I would like
I have an excel file that contain columns of data, my target is to

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.