Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rename columns of a data frame by searching column name

Tags:

r

I am writing a wrapper to ggplot to produce multiple graphs based on various datasets. As I am passing the column names to the function, I need to rename the column names so that ggplot can understand the reference.

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.

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

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?

like image 520
karlos Avatar asked Jul 06 '12 18:07

karlos


People also ask

How would you rename the columns of a data frame with example in R?

Method 1: using colnames() method colnames() method in R is used to rename and replace the column names of the data frame in R. The columns of the data frame can be renamed by specifying the new column names as a vector. The new name replaces the corresponding old name of the column in the data frame.

How do I change the name of a specific column in a DataFrame in Python?

You can change the column name of pandas DataFrame by using DataFrame. rename() method and DataFrame. columns() method.

How can we change the names of the columns in the dataset?

To rename columns, we can pass a dictionary to the columns argument. The keys are the columns you want to change and the values are the new names for these columns. We can also set the argument inplace to True for the change to happen in the existing DataFrame.


2 Answers

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.

like image 139
Jared Avatar answered Nov 02 '22 22:11

Jared


> 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. I suppose you might want to make sure there are a complete set of matches if you were building this into a function.

like image 23
IRTFM Avatar answered Nov 02 '22 23:11

IRTFM