Dongchul Park
Dongchul Park

Reputation: 175

How to remove first two letters of every columns in R?

My data frame looks:

Sdate       Edate    NY_Close_40   NY_High_40
2020-1-3  2020-1-5        20          30

I want to remove "NY_" in every column. I tried by writing

sub("NY_*", "", df[,1])

but my entire data frame is gone. Column names that I want to have is:

Sdate   Edate   Close_40   High_40

What should I change to do it?

Upvotes: 0

Views: 98

Answers (1)

J.C.Wahl
J.C.Wahl

Reputation: 1564

You could do

names(df) = gsub("NY_", "", names(df))

This will replace NY_ with an empty string for all column names in df.

What you are trying to do is to replace NY_ in the first column, not the first column name.

Upvotes: 2

Related Questions