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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:21:45+00:00 2026-05-25T12:21:45+00:00

I am using ddply to split up a data frame and send the chunks

  • 0

I am using ddply to split up a data frame and send the chunks to a function. Before the ddply line, I set i=1. Then inside the function I am incrementing i so that each chunk of data gets a new number. When I run this, however, i is being reset to 1 each time the function is called. I assume this is because the i outside the function is being reassigned each time ddply sends a new chunk of data. Is there a way to increment outside the function and send that number along with the data?

EDIT::
Here is the calling line:

rseDF <- ddply(rseDF, .(TestCompound), .fun = setTheSet)

Here is the function:

##Set The Set Column
setTheSet <- function(df) {
if (df[,"TestCompound"] == "DNS000000001") df[,"Set"] <- "Control"
else {df[,"Set"] <- i
i <<- i+1}
return(df)
}
  • 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-25T12:21:46+00:00Added an answer on May 25, 2026 at 12:21 pm

    That is just a normal scoping issue where you, if you insist on doing this, need to use <<- for the global assignment:

    R> library(plyr)                    ## load plyr
    R> i <- 1                           ## set counter
    R> DF <- data.frame(a=rep(letters[1:3], each=3), b=1:9)
    R> DF                               ## boring but simple data frame
      a b
    1 a 1
    2 a 2
    3 a 3
    4 b 4
    5 b 5
    6 b 6
    7 c 7
    8 c 8
    9 c 9
    R> ddply(DF, .(a), function(x) mean(x$b))     ## summarized
      a V1
    1 a  2
    2 b  5
    3 c  8
    R> ddply(DF, .(a), function(x) { i <<- i + 1; data.frame(i=i, res=mean(x$b)) })
      a i res
    1 a 2   2
    2 b 3   5
    3 c 4   8
    R> 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to make a function using ddply inside of it. However I can't
I am using ddply to aggregate and summarize data frame variables, and I am
I like to write a function using ddply that outputs the summary statistics based
Is it possible to use spaces in ddply? I'm using data from a spreadsheet
I have a set of hospital admission data that I need to process, I
I love the ability of plyr to split a data frame into multiple data
I am using ddply to aggregate my data but haven't found an elegant way
I have written a model that I am fitting to data using ML via
I have a data frame that looks like this: site date var dil 1
Using R's builtin ToothGrowth example dataset, this works: ddply(ToothGrowth, .(supp,dose), function(df) mean(df$len)) But 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.