简体   繁体   English

通过搜索列名重命名数据框的列

[英]Rename columns of a data frame by searching column name

I am writing a wrapper to ggplot to produce multiple graphs based on various datasets. 我正在为ggplot编写一个包装器,以根据各种数据集生成多个图形。 As I am passing the column names to the function, I need to rename the column names so that ggplot can understand the reference. 当我将列名传递给函数时,我需要重命名列名,以便ggplot可以理解引用。

However, I am struggling with renaming of the columns of a data frame 但是,我正在努力重命名数据框的列

here's a data frame: 这是一个数据框:

df <- data.frame(col1=1:3,col2=3:5,col3=6:8)

here are my column names for search: 这是我的搜索列名:

col1_search <- "col1"
col2_search <- "col2"
col3_search <- "col3"

and here are column names to replace: 这里是要替换的列名:

col1_replace <- "new_col1"
col2_replace <- "new_col2"
col3_replace <- "new_col3"

when I search for column names, R sorts the column indexes and disregards the search location. 当我搜索列名时,R对列索引进行排序并忽略搜索位置。

for example, when I run the following code, I expected the new headers to be new_col1, new_col2, and new_col3, instead the new column names are: new_col3, new_col2, and new_col1 例如,当我运行以下代码时,我希望新标题为new_col1,new_col2和new_col3,而新列名称为:new_col3,new_col2和new_col1

colnames(df)[names(df) %in% c(col3_search,col2_search,col1_search)] <- c(col3_replace,col2_replace,col1_replace)

Does anyone have a solution where I can search for column names and replace them in that order? 有没有人有解决方案,我可以搜索列名并按顺序替换它们?

require(plyr)
df <- data.frame(col2=1:3,col1=3:5,col3=6:8)
df <- rename(df, c("col1"="new_col1", "col2"="new_col2", "col3"="new_col3"))
df

And you can be creative in making that second argument to rename so that it is not so manual. 并且您可以创造性地使第二个参数rename以便它不是那么手动。

> names(df)[grep("^col", names(df))] <- 
                        paste("new", names(df)[grep("^col", names(df))], sep="_")
> names(df)
[1] "new_col1" "new_col2" "new_col3"

If you want to replace an ordered set of column names with an arbitrary character vector, then this should work: 如果要用任意字符向量替换一组有序的列名,那么这应该有效:

names(df)[sapply(oldNames, grep, names(df) )] <- newNames

The sapply ()-ed grep will give you the proper locations for the 'newNames' vector. sapply ()- sapply grep将为您提供“newNames”向量的正确位置。 I suppose you might want to make sure there are a complete set of matches if you were building this into a function. 我想如果你把它构建成一个函数,你可能想要确保有一套完整的匹配。

hmm, this might be way to complicated, but the first that come into my mind: 嗯,这可能是复杂的,但第一个进入我的脑海:

lookup <- data.frame(search = c(col3_search,col2_search,col1_search),
                     replace = c(col3_replace,col2_replace,col1_replace))

colnames(df) <- lookup$replace[match(lookup$search, colnames(df))]

I second @justin's aes_string suggestion. 我第二个@ justin的aes_string建议。 But for future renaming you can try. 但是为了将来重命名你可以试试。

require(stringr)
df <- data.frame(col1=1:3,col2=3:5,col3=6:8)
oldNames <- c("col1", "col2", "col3")
newNames <- c("new_col1", "new_col2", "new_col3")
names(df) <- str_replace(string=names(df), pattern=oldNames, replacement=newNames)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM