1

I'm trying to remove the same number of rows from multiple (28) dataframes, each of varying length. Here's a toy dataset to illustrate what I mean:

df1 <- data.frame(
  var1 = c(1,2,3,4,5),
  var2 = c(2,4,6,8,10)
)

df2 <- data.frame(
  var3 = c(1,2,3,4,5,6,7),
  var4 = c(2,4,6,8,10,12,14)
)

df3 <- data.frame(
  var5 = c(1,2,3,4,5,6,7,8,9,10),
  var6 = c(2,4,6,8,10,12,14,16,18,20)
)

I've written a function (below) to do this, which works on individual dataframes:

remove_rows <- function(x){
  x[c(3:nrow(x)),]
}

I've then created a list of the dataframes using df_list <- ls() and tried to run the function across each item in the list using lapply(df_list, function(x) remove_rows(x)), but I keep getting this error: Error in 3:nrow(x) : argument of length 0.

I think it's because my function is trying to act on the item within my list, as opposed to the data frame which the name of the item in my list represents, but I can't work out how to alter it.

I've also tried the hacky approach of a for loop (below), which failed too.

for (i in 1:length(df_list)){
  name <- df_list[i]
  assign(name, remove_rows(df_list[i]))
}
tktk234
  • 426
  • 3
  • 12

2 Answers2

1

We can use mget to get the values of the objects in a list and then apply the remove_rows function

df_list <- lapply(mget(paste0("df", 1:3)), remove_rows)

Or with ls

df_list <- lapply(mget(ls(pattern = "df\\d+")), remove_rows) 
df_list
#$df1
#  var1 var2
#3    3    6
#4    4    8
#5    5   10

#$df2
#  var3 var4
#3    3    6
#4    4    8
#5    5   10
#6    6   12
#7    7   14

#$df3
#   var5 var6
#3     3    6
#4     4    8
#5     5   10
#6     6   12
#7     7   14
#8     8   16
#9     9   18
#10   10   20

NOTE: It is better to keep multiple datasets in a list, but we can also update the original objects in the global environment with list2env (not recommended though)

list2env(df_list, .GlobalEnv)
akrun
  • 874,273
  • 37
  • 540
  • 662
  • So because mine in my actual dataset are all named different, I'd have to use [this answer](http://stackoverflow.com/questions/14954399/put-multiple-data-frames-into-list-smart-way), but that would work well thanks. – tktk234 Feb 11 '17 at 13:14
1

Use a list and lapply your function across each dataframe in the list:

> df_list <- list(df1, df2, df3)
> new_df_list <- lapply(df_list, remove_rows)
> new_df_list
[[1]]
  var1 var2
3    3    6
4    4    8
5    5   10

[[2]]
  var3 var4
3    3    6
4    4    8
5    5   10
6    6   12
7    7   14

[[3]]
   var5 var6
3     3    6
4     4    8
5     5   10
6     6   12
7     7   14
8     8   16
9     9   18
10   10   20
blacksite
  • 12,086
  • 10
  • 64
  • 109
  • Thanks, but because my dataframes all have different names (e.g. not just df1, df2, df3, etc.)/there's a lot of them, I can't manually create a list like `list(df1,df2,df3)`. – tktk234 Feb 11 '17 at 13:10
  • Actually, solved it using [this answer](http://stackoverflow.com/questions/14954399/put-multiple-data-frames-into-list-smart-way) – tktk234 Feb 11 '17 at 13:13