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

  • Home
  • SEARCH
  • 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 8767277
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T16:46:13+00:00 2026-06-13T16:46:13+00:00

Consider any dataframe col1 col2 col3 col4 row.name11 A 23 x y row.name12 A

  • 0

Consider any dataframe

            col1   col2    col3   col4
row.name11    A     23      x       y
row.name12    A     29      x       y
row.name13    B     17      x       y
row.name14    A     77      x       y

I have a list of rownames which I want to return from this dataframe. Lets say I have row.name12 and row.name13 in a list. I can easily return these rows from dataframe. But I also want to return 4 rows above and 4 rows below these rows. It means I want to return from row.name8 to row.name17. I think it is similar to grep -A -B in shell.

Probable solution- Is there any way to return row number by row name? Because if I have row number than I can easily subtract 4 and add 4 in row number and return rows.

Note: Here rownames are just examples. Rownames could be anything like RED, BLUE, BLACK, 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-06-13T16:46:15+00:00Added an answer on June 13, 2026 at 4:46 pm

    Try that:

    extract.with.context <- function(x, rows, after = 0, before = 0) {
    
      match.idx  <- which(rownames(x) %in% rows)
      span       <- seq(from = -before, to = after)
      extend.idx <- c(outer(match.idx, span, `+`))
      extend.idx <- Filter(function(i) i > 0 & i <= nrow(x), extend.idx)
      extend.idx <- sort(unique(extend.idx))
    
      return(x[extend.idx, , drop = FALSE])
    }
    
    dat <- data.frame(x = 1:26, row.names = letters)
    extract.with.context(dat, c("a", "b", "j", "y"), after = 3, before = 1)
    #    x
    # a  1
    # b  2
    # c  3
    # d  4
    # e  5
    # i  9
    # j 10
    # k 11
    # l 12
    # m 13
    # x 24
    # y 25
    # z 26
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

While architecting any n-tier intranet applications, do I have to consider anything about firewalls
I have an Array[Any] from Java JPA containing (two in this case, but consider
Consider following DataTemplate in any List Control: <DataTemplate> <TextBlock Text={Binding} /> </DataTemplate> and following
Consider a web service written in ASP.NET Web API to accept any number files
Consider the following code (for simplicity, I did not follow any C# coding rules).
I am wondering if there are any performance overhead issues to consider when using
Consider any C# program for example, How will you start explaing that program... Friends
Curious about things to consider to avoid any code breaks, etc to upgrade from
Is there any reason to consider XHTML related details for web development for someone
Consider INTEGER,DIMENSION(3) :: NumberVector and INTEGER :: NumberVector(3) Is there any difference whatsoever between

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.