Reputation: 1453
I have a dataframe about stock-market data and I want to delete a specific pattern from all column names. This is my dataframe:
ADS.DE.Open ADS.DE.High ADS.DE.Low ADS.DE.Close ADS.DE.Volume ADS.DE.Adjusted
2017-01-02 149.75 151.95 149.35 151.30 445138 143.6014
2017-01-03 150.90 151.05 148.80 149.25 641172 141.6557
2017-01-04 149.40 149.40 146.35 146.40 618563 138.9507
2017-01-05 145.25 145.80 143.50 144.30 739147 136.9576
2017-01-06 143.80 145.05 142.95 144.80 641054 137.4322
And now I want to remove the "ADS.DE"
pattern from all colums. So that I get the fllowing output:
open high low close volume adjusted
2017-01-02 149.75 151.95 149.35 151.30 445138 143.6014
2017-01-03 150.90 151.05 148.80 149.25 641172 141.6557
2017-01-04 149.40 149.40 146.35 146.40 618563 138.9507
2017-01-05 145.25 145.80 143.50 144.30 739147 136.9576
2017-01-06 143.80 145.05 142.95 144.80 641054 137.4322
I already have a solution that works:
ADS.DE %>%
rename_(.dots=setNames(names(.), tolower(gsub(str_c("ADS.DE","."), "", names(.)))))
But my problem is that I have a lot of dataframes with the same structure and the same problem. I now intend to put my working solution into a map-function. The problem will be the part marked in bold:
ADS.DE %>%
rename_(.dots=setNames(names(.), tolower(gsub(str_c(
"ADS.DE","."), "", names(.)))))
My main concern is tho put my solution into a map-function. Similar to the following:
DAX<-c("ADS.DE","MRK.DE","DB1.DE","EOAN.DE","SAP.DE","BEI.DE","FME.DE","WDI.DE","BMW.DE","HEN3.DE","VOW3.DE","LIN.DE","DBK.DE","FRE.DE","MUV2.DE","DTE.DE","BAYN.DE","RWE.DE","SIE.DE","VNA.DE","ALV.DE","IFX.DE","1COV.DE","DAI.DE","BAS.DE","LHA.DE","CON.DE","HEI.DE","DPW.DE","TKA.DE")
map(mget(DAX),
function(x) x %>% rename_(.dots=setNames(names(.), tolower(gsub(str_c(substitute(x),"."), "", names(.))))))
Can someone help me to solve my problem?
Upvotes: 2
Views: 111
Reputation: 1453
I found a working solution:
map(mget(DAX),function(x)
x%>%rename_all(str_remove , ".+\\..+\\.")%>%rename_all(tolower)
)
Upvotes: 1
Reputation: 4233
This will apply changes to a list of data frames and solve your problem:
df1 <- data.frame(ABS.DE.x = "a", ABS.DE.y = "b")
df2 <- data.frame(ABS.DE.x = "c", ABS.DE.y = "d")
fix_names <- function(list_df){
lapply(list_df, function(df){
setNames(df, stringr::str_replace(names(df), ".+\\..+\\.", ""))
})
}
a <- fix_names(list(df1, df2))
a[[1]]
Upvotes: 1
Reputation: 3909
rename_()
is deprecated and I wouldn't use rename()
either. set_names()
works better.
library(tidyverse)
df <- read_table("
ADS.DE.Open ADS.DE.High ADS.DE.Low ADS.DE.Close ADS.DE.Volume ADS.DE.Adjusted
2017-01-02 149.75 151.95 149.35 151.30 445138 143.6014
2017-01-03 150.90 151.05 148.80 149.25 641172 141.6557
2017-01-04 149.40 149.40 146.35 146.40 618563 138.9507
2017-01-05 145.25 145.80 143.50 144.30 739147 136.9576
2017-01-06 143.80 145.05 142.95 144.80 641054 137.4322")
df %>%
set_names(names(.) %>% str_remove(".+\\..+\\."))
Upvotes: 1
Reputation: 171
try with str_replace from stringr on the names() of the dataframe
library(stringr)
names(df)<-str_replace(names(df), "ADS.DE", "")
Upvotes: 1