Select all items in lists except one

Hi,

I have a list of data frames and I want to iterate over them and for each i merge all data frame except data frame in location i. how should I do it?

this is what i have tried:

for (i in 1: length(df_list)){
          rbind(df_list[[-i]])
}

example:

(df_list <- group_by(iris,Species) %>% group_map(.f = ~head(.),.keep=TRUE))

# 1 setosa
# 2 versicolor
# 3 virginica
# 
# exclude 2

(df_list_to_bind <- bind_rows(df_list[-2]))
2 Likes

Thank you!

You know how can I use the command of bind_rows while keeping the indexes of the original rows?

for example if df_list[[1]] is in indexes 1:30 of the original data frame, bind_rows(df_list[-1])) will start from row 31.

I don't know what you mean by indexes of the original rows in this context, can you relate your request to my example ?

for example: the original data is iris and it has 5000 rows.

after grouping the data and separting to lists according to Species we have 3 different groups.
group number1 was in rows 1:2000 of Iris data
group number2 was in rows 2001:3000 of iris data
group number was in rows 3001:5000 of Iris data

Now I want to merge only groups 2 and 3.
I use the command you offered:
M -> bind_rows(df_list[-1]))

But now data frame M has row Indices of 1:3000 and I want it to be as its original indices - 2001:5000

what you are asking is impossible, as the indexes relate to the contents of the data as it is, not the data how it was.
Do you have a larger issue that you are trying to solve ?
perhaps there is simply a better approach for you.
If you want to know the rownumbers from the original, you simply have to make a column where that is recorded, before you split and through away groups, and when you recombine you will see the rownumbers of the kept rows. but data.frame will always be only directly indexable by its actual rows as a technical fact.

When I'm looking at a specific data frame in the list of data frames, the original row indices are kept. After using the "bind_rows" command it changes.

I know sometimes R doesn't change row indices automatically.

for example if I continue with the example above:
if the original indices of group 2 in Iris data frame are 2001:3000, when I print
I print (df_list[[2]]) I can see that the indices are 2001:3000.

this data.frame is in rows 2001:3000 ? I don't understand..
all dataframes begin on their first row and end on their last row.
the default head value is 6, so df_list[[1]], df_list[[2]],df_list[[3]], are each 6 rows long(
the first 6 of their groups in the order as found in the original iris).

(df_list <- group_by(iris,Species) %>% group_map(.f = ~head(.),.keep=TRUE))

print(df_list[[2]])
# A tibble: 6 x 5
  Sepal.Length Sepal.Width Petal.Length Petal.Width Species   
         <dbl>       <dbl>        <dbl>       <dbl> <fct>     
1          7           3.2          4.7         1.4 versicolor
2          6.4         3.2          4.5         1.5 versicolor
3          6.9         3.1          4.9         1.5 versicolor
4          5.5         2.3          4           1.3 versicolor
5          6.5         2.8          4.6         1.5 versicolor
6          5.7         2.8          4.5         1.3 versicolor

row numbers are 1 to 6. they couldnt be otherwise.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.