suny
suny

Reputation: 119

rename columns names while reading duplicate column names from xls

I am trying to import an xls file from readxl package with duplicate column
names "Class A. a1", "Class A. a1", "Section B. b1", "Section B.
b1","Class B.","Class B." from it.

After reading it as R dataframe the colnames shows as follows "Class A. a1", "Class A. a1__1" ,"Section B. b1" , "Section B. b1__1", "Class B." ,"Class B.__1"

Now I am replacing the "__" with "-" as below

library(readxl)
df <- read_excel("sample.xls",col_names = TRUE) 
names(df) <- gsub(x = names(df), pattern = "__", replacement = "-")

Is there any way to replace identical columns from "__" to "-" while reading itself ,instead of after reading the file.

Upvotes: 0

Views: 1799

Answers (2)

heisenbug47
heisenbug47

Reputation: 184

read_excel() function of readxl package doesn't have that option.

To check what are the arguments the read_excel function accepts type ?readxl::read_excel() in the console or in editor then run it.

It will open the documentation of that function.

Upvotes: 0

Omry Atia
Omry Atia

Reputation: 2443

Let's say I have a file called Book1.xlsx with 3 numeric columns, called A, B and B.

library(read_xl)

read_xlsx("Book1.xlsx", col_names = c("A", "B", "C"), skip = 1)

skip = 1 prevents it from reading the original names which are A, B, and B

Upvotes: 1

Related Questions