0

I have a data frame that I import from excel each week with 40+ columns. Each week the data frame has a different number of columns, I am only interested in the first 40. I take the data frame, drop the columns after 40 and rbind to another data frame (that has 40 columns).

The code I use to drop the columns is"

df = df[ -c(40:45) ] #assume df has 45 columns this week.

I would like to find a step to automate the lendth of columns to drop, similar to length(df$x) type of idea. I try width(df) but doesn't seem to work?

Any ideas please?

  • 2
    you can select first 40 columns with `df = df[1:40]` or drop everything after first 40 columns `df[-c(41:ncol(df))]` – Ronak Shah Sep 04 '20 at 12:55
  • 2
    if you read your excel with `readxl::read_excel`, you can just read in the first 40 columns if you like.. so no need for droppping columns later on... like so: `read_excel(path, range = cell_cols(1:40) )` – Wimpel Sep 04 '20 at 13:12
  • perfect, thank you – 4agreements Sep 04 '20 at 13:33

0 Answers0