how to repeat order for dataframe in r

Viewed 50

I want to do same order as removing the first rows for several dataframes.

lab1 <- lab1[-c(1),]
lab2 <- lab2[-c(1),]
lab3 <- lab3[-c(1),]
lab4 <- lab4[-c(1),]
lab5 <- lab5[-c(1),]
lab6 <- lab6[-c(1),]
lab7 <- lab7[-c(1),]
lab8 <- lab8[-c(1),]
lab9 <- lab9[-c(1),]
lab10 <- lab10[-c(1),]
...

I want to use repeated phase like this.

for(i in 2:19){ labi <- labi[-c(1),]}

However, labi is recognized as a dataframe name. I need to do such orders for many dataframes. Can someone help?

2 Answers

How do you create the dataframes? Is it possible instead to make a list of lab[[i]]?

Else, you can write the expression as a string then evaluate it, but it's a bit of a hacky way, it would be better to avoid it:

cmd <- paste0("lab", 1, "[c(-1),]")
eval(str2expression(cmd))

Maybe you can try list2env like below

list2env(
  lapply(mget(ls(pattern = "lab\\d+")), function(x) x[-1, ]),
  envir = .GlobalEnv
)
Related