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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T02:10:16+00:00 2026-06-16T02:10:16+00:00

There seems to be variations to this question, but none seem to address the

  • 0

There seems to be variations to this question, but none seem to address the situation of being in a loop AND naming and output file. How I thought this might work:

for(j in 1:3) {
    for(k in 1:17){
       extract_[j]km <- extract(RasterStack, SpatialPolygonsDataFrame_[j]km, layer=[k], nl=1, df=TRUE)
    }
}

The extract function is from the raster package. I have already created a series of RasterStacks and SpatialPolygons and I want to pass these to a function ("extract") that has several parameters, some of which I wish to manipulate through the loop, and label the output accordingly. This is a breeze in BASH, but I can’t figure this out in R.

Ultimately, I’d like to pass strings as well, but another post seems to show the way there.

EDIT: I originally posted the above function as being a single dataframe, when in fact, they are specified objects from the raster package (which are ultimately dataframes).

  • 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-16T02:10:17+00:00Added an answer on June 16, 2026 at 2:10 am

    As Justin points out, working with a list is more inline with R’s structure than messing up the workspace with lots of named variables. It quickly becomes challenging to work when you have a lot of objects in the workspace to “know” what’s next.

    Your way:

    for(j in 1:3) { 
    assign(
       paste("extract",j,"km",sep=""), # or paste0 to avoid need for sep=""
       function(
                get(
                    paste("data",j,"km",sep="")
                   )
               )
          )
    }
    

    Personally, I prefer working with lists, so below, I convert your data objects to a list and show you how to run a function on all elements of that list. Working in this way usually relegates the need to use strings in the “get” and “assign” fashion.

    # just converting your variables to a list    
    data.list <- mget(grep("data",ls(),value=TRUE),envir=.GlobalEnv) 
    
    # then output results
    result.list <- lapply(data.list,your_function) 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There seems to be many variations of this question, but none that deal with
There are variations of this question bouncing around SO, but none of them seem
I know there have been plenty of variations on this question before, but none
There seems to be a similar question to this on here but with the
There are a lot of SO questions that seem to address variations of this
Variations of this question have been asked before, but it seems like the issue
I've found a lot of variations to this question within SO, but it seems
I know that variations on this question have been asked, but I've tried all
I have found lot's of variations for this question but just wanted some clarity
Seems there is several variations on this theme. I display an AlertBox to the

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.