Make Sequential Numeric Column Names Prefixed with a Letter

Make sequential numeric column names prefixed with a letter

Here's how I usually do it. sprintf prints the numbers directly. By adding %02d or %03d you can add leading zeroes, which is helpful when dealing with large numbers :D

features <- c(sprintf("f%02d", seq(1,32)),"label")
colnames(urc_training_norm) <- features

How to set column names in R by repeating character?

We can use paste

colnames(df) <- paste0("L", 1:200)

or to make it more automatic

colnames(df) <- paste0("L", seq_along(df))

NOTE: The range (:) operator works for integer, and not with character in base R i.e. 'L1' is a string, while 1 is integer, so 1:200 gives the range of values from 1 to 200

How to name columns with ascending value

You can use paste function and name your columns as follows:

col <- seq(2)
col_names <- paste0("Site", " ", col)
data <- data.frame(a=c(1,2), b=c(2,2))
data

# a b
# 1 1 2
# 2 2 2
names(data) <- col_names
data
# Site 1 Site 2
# 1 1 2
# 2 2 2

Change the number inside the seq which is (2) in this case to the number you want (8000) in your case.

Edit

As per Gregor's suggestion, if the separator you want is a space, you can use paste instead of paste0 as follows.

col <- seq(2)
col_names <- paste("Site", col)
#for a different separator other than space, you can specify sep
col_names <- paste("Site", col, sep = "_")

Label columns with a ascending number

You can use :

names(df) <- paste0('x', seq_along(df), names(df))
df

# x1id x2diet x3period x4score1 x5score2 x6score3
#1 1 A 1 96 1 52
#2 2 B 2 52 93 75
#3 3 A 1 55 50 68
#4 4 B 2 79 3 9
#5 5 A 1 12 6 76
#6 6 B 2 42 86 62

Maybe add an underscore?

names(df) <- paste0('x', seq_along(df), "_", names(df))
names(df)
#[1] "x1_id" "x2_diet" "x3_period" "x4_score1" "x5_score2" "x6_score3"

make column names for only the 4th to 723rd colums prefixed with distance

Since your sheet1 already has column names, we can replace the wanted slice of them with:

colnames(sheet1)[4:723] = colnames(matrix(ncol=720), F, "distance ")

How to rename only certain columns names in a data.frame by adding a prefix?

In base R, we can use startsWith to identify column names which start with a prefix

inds <- startsWith(names(df), "rename")
#Or grep
#inds <- grep("^rename", names(df))
names(df)[inds] <- paste0("finished_", names(df)[inds])

df
# do_not_rename finished_rename1 finished_rename2
#1 1 1 1
#2 2 2 2
#3 3 3 3

R: make column with a sequence of letters for all values in another column greater than threshold

We may use

library(data.table)
setDT(df)[number >= threshold, passed := letters[.I]]

Generating column names with string and number

You can use a list comprehension to generate the column names:

In [66]:
col_list = ['s' + str(x) for x in range(1,21)]
col_list

Out[66]:
['s1',
's2',
's3',
's4',
's5',
's6',
's7',
's8',
's9',
's10',
's11',
's12',
's13',
's14',
's15',
's16',
's17',
's18',
's19',
's20']

After which you can either pass this as the column arg in DataFrame ctor:

In [70]:
df = pd.DataFrame(np.random.randn(5,20), columns=col_list)
df.columns

Out[70]:
Index(['s1', 's2', 's3', 's4', 's5', 's6', 's7', 's8', 's9', 's10', 's11',
's12', 's13', 's14', 's15', 's16', 's17', 's18', 's19', 's20'],
dtype='object')

Or just overwrite the columns attribute by assigning directly:

In [71]:
df = pd.DataFrame(np.random.randn(5,20))
df.columns = col_list
df.columns

Out[71]:
Index(['s1', 's2', 's3', 's4', 's5', 's6', 's7', 's8', 's9', 's10', 's11',
's12', 's13', 's14', 's15', 's16', 's17', 's18', 's19', 's20'],
dtype='object')

You can also use rename or rename_axis but they're for overwriting pre-existing column names for which there is already a related post

You can also add a prefix to a Series created from a range:

In [76]:
col_list = 's' + pd.Series(np.arange(1,21)).astype(str)
df.columns= col_list
df.columns

Out[76]:
Index(['s1', 's2', 's3', 's4', 's5', 's6', 's7', 's8', 's9', 's10', 's11',
's12', 's13', 's14', 's15', 's16', 's17', 's18', 's19', 's20'],
dtype='object')


Related Topics



Leave a reply



Submit