Cleaning data in R using for loops

by Drew   Last Updated May 23, 2020 00:26 AM

I've been banging my head against the wall with this problem, and I would love your help. To illustrate, I've created two dummy datasets (data A and data B).

dataA <- data.frame(TimeofLife = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15),
                    TimeofDeath = c(4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18))
dataB <- data.frame(Time = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15),
                    Value = c(500, 510, 520, 530, 540, 500, 510, 520, 530, 540, 500, 510, 520, 530, 540))

Below are the first 6 values of dataA

  TimeofLife TimeofDeath
1          1           4
2          2           5
3          3           6
4          4           7
5          5           8
6          6           9

and dataB

   Time Value
1    1   500
2    2   510
3    3   520
4    4   530
5    5   540
6    6   500

I want to use the information I have in dataA to create a subset in dataB. For example, the first row of dataA has the values (1, 4). Using these as the range of values, I want to create a subset for dataB that looks like this:

  Time Value
1    1   500
2    2   510
3    3   520
4    4   530

Now, the issue that I am having is that in my real dataset, I need to create many subsets (thousands+), and I cannot do this manually. That is, I cannot hard-code for each subset because that'd be a nightmare and error-prone.

What I'd like to do is create a for loop which I can use to extract information that I need from dataA and use it to create subsets in dataB.

The logic of the code I want looks something like this (this is nonsense, bc I don't know how to code it):

for(row i in dataA)
  {find the values of TimeofLife and Time of Death in row dataA[i],
  then use those values to filter a subset of dataB, 
  extract and save this subset as a dataframe, 
  do it for all [i]}

Thank you so much.



Related Questions


looping over different factor levels in a dataframe

Updated December 08, 2017 01:26 AM

Changing from a loop to a function in R lapply

Updated May 23, 2018 00:26 AM

Run a function in loop on a data frame of 3 values

Updated March 03, 2019 08:26 AM