Rename a Sequence of Variable Names in Data Frame

rename sequence numeric data frame column within list

Consider building a corresponding same structure and length object, my.nums, of needed numbers and then run a double Map (wrapper to mapply). This avoids the need to sequentially grow and recall an integer variable with i=i+1. Below demonstrates with random data.

set.seed(7182018)
my.data <- list(data.Precip = replicate(7, data.frame(Date = 1:36, col = runif(36, 0, 50)), simplify = FALSE),
data.Rad = replicate(7, data.frame(Date = 1:36, col = runif(36, 0, 50)), simplify = FALSE),
data.Velvi = replicate(7, data.frame(Date = 1:36, col = runif(36, 0, 50)), simplify = FALSE),
data.Temp = replicate(7, data.frame(Date = 1:36, col = runif(36, 0, 50)), simplify = FALSE))

# LONG FORM
my.nums <- list(names.Precip = lapply(1:7, identity),
names.Rad = lapply(8:14, identity),
names.Velvi = lapply(15:21, identity),
names.Temp = lapply(22:28, identity))
# SHORT FORM
my.nums <- lapply(seq(1,28, by=7), function(x) lapply(seq(x, x+6), identity))

my.names <- list(names.Precip = lapply(rep("PRECIPITACIO_", 7), identity),
names.Rad = lapply(rep("RADIACION_", 7), identity),
names.Velvi = lapply(rep("VELOCIDAD.VIENTO_", 7), identity),
names.Temp = lapply(rep("TEMPERATURA_", 7), identity))
# FUNCTIONS
name_func <- function(df, m, n) setNames(df, c("Date", paste0(m, n)))
iter_df <- function(df_lst, m_list, n_lst) Map(name_func, df_lst, m_list, n_lst)

my.new.data <- Map(iter_df, my.data, my.names, my.nums)

Output

lapply(my.new.data, function(lst) lapply(lst, names))

$data.Precip
$data.Precip[[1]]
[1] "Date" "PRECIPITACIO_1"

$data.Precip[[2]]
[1] "Date" "PRECIPITACIO_2"

$data.Precip[[3]]
[1] "Date" "PRECIPITACIO_3"

$data.Precip[[4]]
[1] "Date" "PRECIPITACIO_4"

$data.Precip[[5]]
[1] "Date" "PRECIPITACIO_5"

$data.Precip[[6]]
[1] "Date" "PRECIPITACIO_6"

$data.Precip[[7]]
[1] "Date" "PRECIPITACIO_7"

$data.Rad
$data.Rad[[1]]
[1] "Date" "RADIACION_8"

$data.Rad[[2]]
[1] "Date" "RADIACION_9"

$data.Rad[[3]]
[1] "Date" "RADIACION_10"

$data.Rad[[4]]
[1] "Date" "RADIACION_11"

$data.Rad[[5]]
[1] "Date" "RADIACION_12"

$data.Rad[[6]]
[1] "Date" "RADIACION_13"

$data.Rad[[7]]
[1] "Date" "RADIACION_14"

$data.Velvi
$data.Velvi[[1]]
[1] "Date" "VELOCIDAD.VIENTO_15"

$data.Velvi[[2]]
[1] "Date" "VELOCIDAD.VIENTO_16"

$data.Velvi[[3]]
[1] "Date" "VELOCIDAD.VIENTO_17"

$data.Velvi[[4]]
[1] "Date" "VELOCIDAD.VIENTO_18"

$data.Velvi[[5]]
[1] "Date" "VELOCIDAD.VIENTO_19"

$data.Velvi[[6]]
[1] "Date" "VELOCIDAD.VIENTO_20"

$data.Velvi[[7]]
[1] "Date" "VELOCIDAD.VIENTO_21"

$data.Temp
$data.Temp[[1]]
[1] "Date" "TEMPERATURA_22"

$data.Temp[[2]]
[1] "Date" "TEMPERATURA_23"

$data.Temp[[3]]
[1] "Date" "TEMPERATURA_24"

$data.Temp[[4]]
[1] "Date" "TEMPERATURA_25"

$data.Temp[[5]]
[1] "Date" "TEMPERATURA_26"

$data.Temp[[6]]
[1] "Date" "TEMPERATURA_27"

$data.Temp[[7]]
[1] "Date" "TEMPERATURA_28"

How to conditionally rename multiple columns of a dataframe using values from a sequence as new variable names?

Perhaps colnames can do the job

colnames(df) <- var_names

Just a remark: It is not the best idea to have numbers as colnames, as it can be confusing and not easy to work with (for example you need to quote such names with ` `)

Rename variables for multiple dataframe, using a loop, referencing the dataframe names from a list

We can get the values of the object names with mget into a list, loop over the list with lapply, set the column names to replicated 'VALUE' (not recommended at all - as data.frame column names should be unique)

lst1 <- lapply(mget(df_list), function(x) setNames(x, rep("VALUE", ncol(x))))

Rename all variables that contai a particular string and add a sequencial number

Update

From dplyr 1.0.0 you can use rename_with.

You can select columns to rename by position

library(dplyr)
ds %>% rename_with(~paste0("var", seq_along(.), sub("nameverybig_*", "_", .)), -1)

Or by name

ds %>% rename_with(~paste0("var", seq_along(.), sub("nameverybig_*", "_", .)), 
starts_with('nameverybig'))

Both of which return :

#   identification var1_do_you_like_cookies var2_have_you_been_in_europe var3_whats_your_gender
#1 1 1 1 1
#2 2 2 2 2
#3 3 3 3 3
#4 4 4 4 4
#5 5 5 5 5
#6 6 6 6 6
#7 7 7 7 7
#8 8 8 8 8
#9 9 9 9 9
#10 10 10 10 10

Old Answer

You could use paste0 with sub

ds %>% rename_all(~paste0("var", seq_along(.), sub("nameverybig_*", "_", .)))

To rename only specific variable we can use rename_at

ds %>% rename_at(vars(starts_with("nameverybig")), 
~paste0("var", seq_along(.), sub("nameverybig_*", "_", .)))

Rename multiple columns by names

setnames from the data.tablepackage will work on data.frames or data.tables

library(data.table)
d <- data.frame(a=1:2,b=2:3,d=4:5)
setnames(d, old = c('a','d'), new = c('anew','dnew'))
d

# anew b dnew
# 1 1 2 4
# 2 2 3 5

Note that changes are made by reference, so no copying (even for data.frames!)

Rename specific columns across multiple data sets

This is what you are looking for:

 dat=lapply(list(df2=df2,df3=df3),function(x){names(x)[which(names(x)=="help")]="var2";x})
list2env(dat,.GlobalEnv)

Pandas rename column by position?

try this

df.rename(columns={ df.columns[1]: "your value" }, inplace = True)


Related Topics



Leave a reply



Submit