1

I have an Excel that I need to load into R.

Excel have columns for example - A,B,C,D,E I am trying to load it and arrange the excel in the below way-

df1 <- read_excel("abc.xlsx") %>% 
       arrange(A)  

The above code would load the excel with columns A,B,C,D,E and arrange it on basis of A Asc.

Is there any way I can delete few columns like D,E or C,E or B,C while loading that excel itself?

kaydee
  • 67
  • 5
  • Is this helping? https://stackoverflow.com/questions/51886004/keeping-specific-columns-in-read-excel – elielink Aug 10 '21 at 13:48

1 Answers1

0

Yes, it is possible to only read a range of columns. In this case i read only the first 3 columns (1:3)

df1 <- read_excel("abc.xlsx", range = cell_cols(1:3)) %>% 
       arrange(A) 

Because you changed your question now an edit:

If you want to read random cells like A:C and F:G you can also use cell_cols. You would then need 2 lines of code and then merge it together. As i understanbd you want to provide the Letters, so this is possible:

df1 <- read_excel("abc.xlsx", range = cell_cols("A:C"))
df2 <- read_excel("abc.xlsx", range = cell_cols("F:G"))
df <- cbind(df1, df2)

At the end, read_excel is not providing a solution for your problem. I think you would need to write a function that gets the Ranges between the letters and then reads one range after another. OR: You use another library: here is a solution R read excel by column names

pbraeutigm
  • 455
  • 4
  • 8
  • What if I have to pick random columns to delete. – kaydee Aug 10 '21 at 14:06
  • I edited my answer. Is this your wanted solution? – pbraeutigm Aug 11 '21 at 07:09
  • Actually using cell_cols() is better when you have to select some columns in series. I have edited my question - the last line in Bold, what if out of columns A,B,C,D,E,F,G,H,I,J I dont want columns D,G,I,B ?(picking up columns that I don't want Randomly OR I don't want alternate columns) I hope I am now clear with my question. – kaydee Aug 11 '21 at 13:32
  • I edited my answer again. Hope it satifies you. – pbraeutigm Aug 11 '21 at 14:13
  • Thanks a ton! you rock =) – kaydee Aug 11 '21 at 14:41