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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:16:13+00:00 2026-05-23T07:16:13+00:00

I have this function in R from a previous question here shift <- function(d,

  • 0

I have this function in R from a previous question here

shift <- function(d, k) rbind( tail(d,k), head(d,-k), deparse.level = 0 )

this function will rotate the data frame d by K, that’s mean it will take K rows from the end of the data frame and place them on the top.

I want to create the same function(in the same language) but without using R pre-made functions(head, tail,…), but only using basics of programming.(for , …)

How this can be done?

  • 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-23T07:16:14+00:00Added an answer on May 23, 2026 at 7:16 am

    Well I don’t know what you mean with without using R functions since pretty much everything is an R function, but here is a solution using only the very generic nrow() (Number of rows of a matrix), %% (modulus) and seq_len (equivalent to 1:length(x) except that it works better):

    m <- matrix(1:40,,2,byrow=TRUE)
    
    shift2 <- function(d, k) d[(seq_len(nrow(d))-k-1)%%(nrow(d))+1,]
    
    shift2(m,5)
    
    
          [,1] [,2]
     [1,]   31   32
     [2,]   33   34
     [3,]   35   36
     [4,]   37   38
     [5,]   39   40
     [6,]    1    2
     [7,]    3    4
     [8,]    5    6
     [9,]    7    8
    [10,]    9   10
    [11,]   11   12
    [12,]   13   14
    [13,]   15   16
    [14,]   17   18
    [15,]   19   20
    [16,]   21   22
    [17,]   23   24
    [18,]   25   26
    [19,]   27   28
    [20,]   29   30
    

    If you mean with “normal programming code” that it shouldn’t be vectorized then, well, you are learning either the wrong language in the right way or the right language in the wrong way. Everytime you come up with a vectorized solution instead of for loops you are happy in R.

    But if you really really want to do this with loops here is exactly the same function unvectorized:

    shift3 <- function(d, k) 
    {
      out <- matrix(,nrow(d),ncol(d))
      sorts <- (seq_len(nrow(d))-k-1)%%(nrow(d))+1
      for (i in seq_len(nrow(d))) out[i,] <- d[sorts[i],]
      return(out)
    }
    

    Proof they are all equal:

    all(shift(m,5) == shift2(m,5) & shift2(m,5) == shift3(m,5))
    [1] TRUE
    

    EDIT:

    Actually shift3() there STILL contained a lot of vectorizations, showing just how native that is in R. Here is a fully unvectorized version:

    shift3 <- function(d, k) 
    {
      out <- matrix(,nrow(d),ncol(d))
      sor <- numeric(1)
      for (i in seq_len(nrow(d))) 
      {
        if (i-k < 1) sor <- nrow(d)-k+i else sor <- i-k
        for (j in seq_len(ncol(d))) out[i,j] <- d[sor,j]
      }
      return(out)
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this function from a plugin (from a previous post) // This method
All, this is a follow up from a previous question here: C# formatting external
This is a related to a previous question I have asked here, see the
I have this function to read in all ints from the file. The problem
I have a JavaScript function, pop_item . I have to call this from PHP,
I have this function in my head: <head> window.onload = function(){ var x =
Okay, this is following on from my previous question reguarding performing a simple ajax
This is a follow up to a previous question which seems to have confused
I have this function in my Javascript Code that updates html fields with their
I have this function... private string dateConvert(string datDate) { System.Globalization.CultureInfo cultEnGb = new System.Globalization.CultureInfo(en-GB);

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.