Skip to content

Help with looping a function over a list of dataframes:

5 messages · Eric Berger, Bert Gunter, Kathan Desai

#
---------- Forwarded message ---------
From: Kathan Desai <kdesai1 at sheffield.ac.uk>
Date: Sat, 4 Jul 2020 at 14:31
Subject: Re: [R] Help with looping a function over a list of dataframes:
To: Jim Lemon <drjimlemon at gmail.com>


Hi Jim,

Thankyou for your reply, I tried the function you suggested and it
doesn't seem to work. There are again no error messages produced, however
the transformation to each position_tab_n table isn't being applied.

Cheers,
Kathan
On Sat, 4 Jul 2020 at 11:14, Jim Lemon <drjimlemon at gmail.com> wrote:

            

  
  
#
Hi Kathan,
How about trying to create a *minimal* reproducible example, e.g. with a
list of two data frames, where each data frame has 5 rows,?
My guess is that there is a good chance that when you try to create such an
example, you will discover the problem yourself.
In the event that you create the example but still cannot solve your issue,
you will find more people on this list willing to look into your question,
as it will be much faster for them to do that (compared to the original
formulation.)

Eric
On Sat, Jul 4, 2020 at 4:33 PM Kathan Desai <kdesai1 at sheffield.ac.uk> wrote:

            

  
  
#
*I hope this is more succinct.*

*I have the following code: *
list_df$position_tab_5$ID <- unlist(lapply(list_df$position_tab_5$midpoint,
function(x) ifelse(any(abs(x - list_df$position_tab_4$midpoint) <= 1),1,0)))


It compares every observation from the midpoint column from dataframe 2 to
every observation from the midpoint column from dataframe 1. It does this
in order to find any values within +/- 1 of the value, and if it satisfies
this criteria then it assigns it a unique id of 1. If not then a value of
0.

In the example below, pos_tab_5 is being compared to pos_tab_4:

Pos tab 4:
Object minimum maximum midpoint
2600    4             22             13
2604    42           85             63.5
2606    142         172           157
2609    223         241           232
2611    393         421           407

Pos tab 5:
Object minimum maximum midpoint ID
2580    3             21             12           1
2581    43           85             64           1
2585    132         168           150         0
2586    223         241           232         1
2589    391         419           405         0

The reason it compares every observation from pos_tab_(n) to pos_tab_(n-1)
is because some of the dataframes are of different row length, and so by
comparing every observation from one DF to another, it can find any values
that are within +/- 1 of each other. (which is the main thing i'm looking
for)

I need help looping this function over a list of dataframes. The list of
dataframes called: list_df contains 121 different dataframes all
representing a different time point of the object.

This is what i have so far:
for(i in seq_along(list_df)){
   list_df$position_tab_[[i]]$ID <-
     unlist(lapply(list_df$position_tab_[[i]]$midpoint, function(x)
                 ifelse(any(abs(x - list_df$position_tab_[[i-1]]$midpoint)
<= 1),1,0)
            ))
}
On Sat, 4 Jul 2020 at 14:44, Eric Berger <ericjberger at gmail.com> wrote:

            

  
  
#
Perhaps the following will be helpful (you can ignore the warning message
here):
Warning message:
In x - y : longer object length is not a multiple of shorter object length
flag
 [1,] 3 2    1
 [2,] 3 2    1
 [3,] 4 4    1
 [4,] 4 3    1
 [5,] 4 4    1
 [6,] 4 1    0
 [7,] 4 2    0
 [8,] 5 3    0
 [9,] 5 5    1
[10,] 3 1    0

Do note that all columns in a data frame must have the same length, so
maybe you'll need to pad with NA's -- I do not entirely get what you are
trying to do.

Bert Gunter

"The trouble with having an open mind is that people keep coming along and
sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
On Sat, Jul 4, 2020 at 8:04 AM Kathan Desai <kdesai1 at sheffield.ac.uk> wrote:

            

  
  
#
Hello thanks everyone for your help i managed to get a working function as
followed:

for(i in 2:length(list_df)){
list_df[[paste0("position_tab_",i)]][['ID']] <-
 unlist(lapply(list_df[[paste0("position_tab_",i)]][['midpoint']],
function(x)
  ifelse(any(abs(x - list_df[[paste0("position_tab_",i-1)]][['midpoint']])
                       <= 1),1,0)         )) }

The idea with this was to detect stationary mitochondria from each frame.
So i needed to link objects from frame to frame (pos_tab = one frame) and
find any of them that didnt move more than 2 pixels hence the +/- one
requirement.
On Sat, 4 Jul 2020 at 16:25, Bert Gunter <bgunter.4567 at gmail.com> wrote: