Add a prefix to column names
You have misread the help file. Here's the argument to look at:
do.NULL
: logical. If FALSE
and names are NULL
, names are created.
Notice the and in that description. Your names are no longer NULL
, so using prefix won't work.
Instead, use something like this:
> m2 <- cbind(1,1:4)
> colnames(m2) <- c("x","Y")
> colnames(m2) <- paste("Sub", colnames(m2), sep = "_")
> m2
Sub_x Sub_Y
[1,] 1 1
[2,] 1 2
[3,] 1 3
[4,] 1 4
How to add a suffix (or prefix) to each column name?
You can use a list
comprehension:
df.columns = [str(col) + '_x' for col in df.columns]
There are also built-in methods like .add_suffix()
and .add_prefix()
as mentioned in another answer.
How to add prefix to column names except some columns?
Another option, use list comprehension on the dataframe column header object, this way you aren't manipulating nor copying the entire dataframe:
df.columns = [f'new_{i}' if i not in ['Name', 'Id'] else f'{i}' for i in df.columns]
df
Output:
Id Name new_Age new_Status
0 1 Alex 22 single
1 2 Bob 32 married
2 3 Clarke 23 single
How to add prefix to column name according to data in another column
Try this:
import pandas as pd
data = {
'ID': [1, 2, 3, 4, 5, 6, 7, 8],
'LABEL': ['text', 'logo', 'logo', 'person', 'text', 'text', 'person', 'logo'],
'cluster_label': ['c_0', 'c_0', 'c_0', 'c_1', 'c_1', 'c_2', 'c_2', 'c_3']
}
df = pd.DataFrame(data)
pd.get_dummies(df,columns=['cluster_label'])
df['dummy'] = df.apply (lambda row: row['LABEL']+'_'+row['cluster_label'], axis=1)
pd.get_dummies(df['dummy'])
## If you want to keep ['ID','LABEL','cluster_label'] in your df :
df = df.join(pd.get_dummies(df['dummy']))
Add prefix to the columns in a slice of the dataframe
You cannot directly assign a prefix as you are currently doing it, given that indices do not support mutable operations. So you would have to reassign all columns again. Here's one way to do it with a list comprehension:
df.columns = ['f_' + i if ix < 15 else i for ix, i in enumerate(df.columns)]
Adding a prefix to certain column names
Try this:
colnames(m2)[1] <- paste0("Sub", "_", colnames(m2)[1])
# or if you prefer paste
#colnames(m2)[1] <- paste("Sub", colnames(m2)[1], sep = "_")
Adding prefix to specific R columns
You aren't obliged to use grepl()
. It's possible to use gsub()
instead. Since gsub()
if no match found, returns unchanged values, you can change your column names on the fly, without intermediate indexing step:
Example data set
sed.seed(66248086)
(
dat <- data.frame(
ID = 1:3,
EXPFRQ = runif(3),
EXPMETADAT = sample(c(T, F), 3, T),
.EXPGRP = sample(LETTERS[1:3], 3, T),
EXPOUT = sample(c('*', '**', '***'), 3, T),
NOTES = c("Bad project...", "What is in this tube?", "Blot, blot western baby")
)
)
# ID EXPFRQ EXPMETADAT .EXPGRP EXPOUT NOTES
# 1 1 0.5483680151 FALSE C * Bad project...
# 2 2 0.0628816469 FALSE B ** What is in this tube?
# 3 3 0.0001267055 TRUE B *** Blot, blot western baby
Actually where renaming takes place
Just give to gsub()
your pattern, and call the chunk it found using back-reference (\\1
):
colnames(dat) <- gsub('^(EXP[A-Z]{3})$', 'New_\\1', colnames(dat))
Result
dat
# ID New_EXPFRQ EXPMETADAT .EXPGRP New_EXPOUT NOTES
# 1 1 0.5483680151 FALSE C * Bad project...
# 2 2 0.0628816469 FALSE B ** What is in this tube?
# 3 3 0.0001267055 TRUE B *** Blot, blot western baby
Note: Please, reade the note written by @akrun under his answer!
Adding a numbered prefix to column names in a for loop
Put them in a list and use their name (df1, df2, etc...)to catch the prefix, i.e.
l1 <- mget(grep(pattern = "df[0-9]+", x = ls(), value = TRUE))
Map(function(x, y) setNames(x, paste0('MTH', gsub('\\D+', '', y), '_', names(x))),
l1, names(l1))
$df1
MTH1_v1 MTH1_v2
1 5 9
2 6 10
3 7 11
$df2
MTH2_v1 MTH2_v2
1 15 19
2 16 110
3 17 111
To change all names except the first one then,
Map(function(x, y) data.frame(x[1], setNames(x[-1], paste0('MTH', gsub('\\D+', '', y), '_', names(x)[-1]))), l1, names(l1))
$df1
v1 MTH1_v2
1 5 9
2 6 10
3 7 11
$df2
v1 MTH2_v2
1 15 19
2 16 110
3 17 111
DATA
dput(df1)
structure(list(v1 = c(5, 6, 7), v2 = c(9, 10, 11)), class = "data.frame", row.names = c(NA,
-3L))
dput(df2)
structure(list(v1 = c(15, 16, 17), v2 = c(19, 110, 111)), class = "data.frame", row.names = c(NA,
-3L))
Adding a suffix to a selection of column names in tidyverse
You may select columns in rename_with
-
library(dplyr)
df %>% rename_with(~paste0("a", .x), c(x, y))
# ax ay z
#1 1 3 5
#2 2 4 6
Related Topics
Display a Time Clock in the R Command Line
Extract Matrix Column Values by Matrix Column Name
How to Use a String Variable to Select a Data Frame Column Using $ Notation
Force Character Vector Encoding from "Unknown" to "Utf-8" in R
Alternative to Expand.Grid for Data.Frames
Add "Filename" Column to Table as Multiple Files Are Read and Bound
What Are the R Sorting Rules of Character Vectors
How to Specify the Actual X Axis Values to Plot as X Axis Ticks in R
Add Empty Columns to a Dataframe with Specified Names from a Vector
Why Is Apply() Method Slower Than a for Loop in R
Chi Square Analysis Using for Loop in R
How to Source R Markdown File Like 'Source('Myfile.R')'
Short Formula Call for Many Variables When Building a Model
Find the Intersection of Overlapping Ranges in Two Tables Using Data.Table Function Foverlaps