简体   繁体   中英

how to repeat order for dataframe in r

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?

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))

Thank you! I make dataframes from excel files.

lab1 <- readxl::read_excel(path="001.xlsx", sheet="sheet1", col_names=FALSE)
lab2 <- readxl::read_excel(path="002.xlsx", sheet="sheet1", col_names=FALSE)

I have an error for making lab[[1]] . I think I need an emtpy dataframe. Each file has different number of rows. But, same name of columns. How can I make the dataframe for this case?

Maybe you can try list2env like below

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM