2016-01-18 74 views
1

假設我想通過在數據幀的以下列表中刪除特殊字符重新命名列:重命名與lapply列表列和GSUB

df1 <- data.frame("x.1" = runif(3), "y.1" = runif(3)) 
df2 <- data.frame("x.2" = runif(3), "y.2" = runif(3)) 
df.list <- list(df1, df2) 

我試過下面的代碼..

dplyr::df.list <- lapply(df.list, function(y) gsub("\\.", "", colnames(y))) 

我怎樣才能lapply()gsub()刪除特殊字符?

回答

2

試試這個:

df.list <- lapply(df.list, function(y) {colnames(y) <- gsub("\\.", "", colnames(y)); y}) 
df.list