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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T13:33:54+00:00 2026-06-14T13:33:54+00:00

I am nearly new to R, so sorry if I make some basic questions,

  • 0

I am nearly new to R, so sorry if I make some basic questions, but I can not find a solution to this “simple” problem:
Having a database (big one, 25 million rows, 14 cols) of patients, I have several rows for each “id”, with for example this structure:

"id" "birth_date"  "treatment"  "date_treatment"
123   2002-01-01    2            2011-01-03
123   2002-01-01    3            2011-10-03
124   2002-01-01    6            2009-11-07
124   2002-01-01    NA           NA
...   .....         ......       ........ 
1022  2007-01-01    4            2011-01-06

I have to use ff package to be able to work with little amount of RAM, so ALL the processes should be into ff functions.
And I want to know, for each single “id”, which is the minimum “age” when he/she received
a treatment = 2 or 4. so, that would be, in each single id, in generic code :

if(treatment in c(2,4)) then min(date_treatment – birth_date)

I only want to keep those minimum “ages” data and perhaps the ids.

One solution is to do:

age_c <- (data$date_treatment - data$birth_date)/365.25;
data$age_c <- age_c;
idx <- ffwhich( data, treatment %in% c(2,4) );
result  <- data[idx,];

This keeps all the process into ff, and no memory problems, but…
I still need to find a way to take those minimums ages for each id…
ffdfdply seems to be able to do that:

age_fun <- function(x){ 
  min_ <- min.ff(x$age_c); 
  data.frame( age = min_);  
}

 result2 <- ffdfdply(x = data,
               split = data$id,
               FUN = function(x) age_fun(x),
               BATCHBYTES = 5000,
               trace=TRUE
 ); 

Which takes looooong time and also gives me a lot of different errors….

Any solution to that?
It is a general problem that in SAS or SQL are easy to do, but i do not find the right combination in R.
So the general question would be:

how to compute row-column functions for identical values (groups) of a variable (row) in
very big data sets ???

Thanks !!

  • 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-14T13:33:55+00:00Added an answer on June 14, 2026 at 1:33 pm

    ffdfdply is the function you need to solve your question but you are using it wrong and inefficiently. Think about ffdfdply as getting in each FUN, the maximum number of data R allows you to put in RAM but still making sure you get all your data by each id in RAM (or possibly several id’s if it fits into RAM).

    So taking BATCHBYTES 5000 is rather small (do you really only have 5 kilobytes of RAM – I guess not – did you install R on a Commodore from the 90’s?) Next, your FUN age_fun is written wrongly. To see what you get in the FUN you can print it out. as in FUN=function(x){ print(head(x))); x}.
    In FUN, you get data in RAM, so you don’t need to use min.ff, min will do.

    Also note the remark of joran: you get multiple id’s in each chunk if your RAM allows to. Make sure your FUN does a split-apply-combine strategy or use dply in FUN.
    And another remark to speed things up. Do you really need to pass the whole ffdf. You only need the columns you use in the function and the split. So ffdfdply(x = data[c(“id”,”age_c”,”treatment”)], split = …) will do otherwise you get data in RAM which is not needed.

    So to be short, something like this will do the trick

    require(doBy)
    result2 <- ffdfdply(
      x = data[c("id","age_c","treatment")], split = data$id,
      FUN = function(x) summaryBy(age_c ~ id, data=subset(x, treatment %in% c(2,4)), FUN=min))
    

    If you also want to have your persons who did not have treatment 2 and 4 whatsover, do like this.

    require(doBy)
    result2 <- ffdfdply(
      x = data[c("id","age_c","treatment")], split = data$id,
      FUN = function(x) {
       persons <- unique(x[, "id", drop=FALSE])
       result <- merge(
         persons,
         summaryBy(age_c ~ id, data=subset(x, treatment %in% c(2,4)), FUN=min),
         by.x="id", by.y="id", all.x=TRUE, all.y=FALSE
         )
       result
    })
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have searched google up and down but I can not find nearly any
(Nearly solved, but not quite) (My temporary solution is to use a void method
I'm new to git; I know the basic commands, but I'm not as familiar
ok so i've nearly got this. But it seems there is some logic error
Hi I'm nearly new with this of Regex... so, maybe my problem it's easy
I'm new to Django but I seem to have nearly identical code working on
Server creates new thread in a threadpool. This thread reads some stuff into buffer
I am trying to create a new node type like this: http://www.live-wtr.ru/leo/Tikz9.png but don't
EDIT Leaving this for posterity, but nearly a year later, to get down voted,
Sorry for such a general questions, but I'm wondering if (and how) it is

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.