How to Rename All Columns of a Data Frame Based on Another Data Frame in R

Rename columns of a dataframe based on another dataframe except columns not in that dataframe in R

A solution using the rename_with function from the dplyr package.

library(dplyr)

df3 <- df2 %>%
filter(Col1 %in% names(df1))

df4 <- df1 %>%
rename_with(.cols = df3$Col1, .fn = function(x) df3$Col2[df3$Col1 %in% x])

df4
# E Q R Z G
# 1 1 2 3 4 5

How can I rename all columns of a data frame based on another data frame in R?

A    B    C   D
1 2 3 4

DataFrame1 <- read.table(con <- file("clipboard"), header=T)

Col1 Col2
A E
B Q
C R
D Z

DataFrame2 <- read.table(con <- file("clipboard"), header=T)

colnames(DataFrame1) <- DataFrame2$Col2

If the column names didn't go in order like they do in the example you'd have to use match:

DataFrame2$Col2[match(names(DataFrame1),DataFrame2$Col1)]

Rename columns based on another DataFrame in R

If I understand correctly, this should work:

names(df1)[1:39] <- names(dfnorm)[1:39]

Rename all files in a directory based off of columns in another index data frame

You could loop over the rows, each time using paste0() to create a mv command, which is then provided to system()

purrr::walk(1:nrow(df),function(i) {
cmd = paste0("mv dir1/",df[["sn"]][i], ".csv dir1/", df[["location"]][i], ".csv")
system(command=cmd)
})

Change column names in dataframe based on matching to another dataframe by dplyr

We could reshape wide-to-long, merge, then reshape again to long-to-wide:

library(dplyr)
library(tidyr)

pivot_longer(df, cols = starts_with("S"), names_to = "Sample_name") %>%
left_join(df2, by = "Sample_name") %>%
pivot_wider(id_cols = Gene_Symbol, names_from = Tumor_name, values_from = value)

## A tibble: 7 x 8
# Gene_Symbol Tumor56 Tumor17 Tumor99 Tumor2 Tumor34 Tumor84 Tumor51
# <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#1 Gene1 85658. 10423. 18779. 23920. NA NA NA
#2 Gene2 54418. 41661. 43656. 47829. 46677. 68596. 80506.
#3 Gene3 110949. 40095. NA NA 63389. 56803. 48723.
#4 Gene4 53197. 49520. 57447. 51479. 48722. 44713. 38629.
#5 Gene5 87157. 129387. 113266. 116275. NA NA NA
#6 Gene6 NA NA 44810. 43111. 77136. 47744. 37885
#7 Gene7 23880. 23903. 26317. 25417. 40266. 33690. 36638.

Replacing column names with another data frame if matches

If you are open to a tidyverse solution, you could use

library(dplyr)
library(tibble)

df %>%
rename_with(~deframe(df2)[.x], .cols = df2$Name) %>%
select(Name, Reference, any_of(df2$Adjusted_Name))

This returns

# A tibble: 3 x 6
Name Reference good_run very_great_work bad_run fair_run_decent
<chr> <chr> <dbl> <dbl> <dbl> <dbl>
1 George Hill 34 21 33 21
2 Frank Stairs 29 30 29 28
3 Bertha Trail 25 21 24 25

Data

df <- structure(list(Name = c("George", "Frank", "Bertha"), Reference = c("Hill", 
"Stairs", "Trail"), Good = c(34, 29, 25), Fair = c(21, 28, 25
), Bad = c(33, 29, 24), Great = c(21, 30, 21), Poor = c(32, 29,
26)), class = c("spec_tbl_df", "tbl_df", "tbl", "data.frame"), row.names = c(NA,
-3L), spec = structure(list(cols = list(Name = structure(list(), class = c("collector_character",
"collector")), Reference = structure(list(), class = c("collector_character",
"collector")), Good = structure(list(), class = c("collector_double",
"collector")), Fair = structure(list(), class = c("collector_double",
"collector")), Bad = structure(list(), class = c("collector_double",
"collector")), Great = structure(list(), class = c("collector_double",
"collector")), Poor = structure(list(), class = c("collector_double",
"collector"))), default = structure(list(), class = c("collector_guess",
"collector")), skip = 1L), class = "col_spec"))

df2 <- structure(list(Name = c("Good", "Great", "Bad", "Fair"), Adjusted_Name = c("good_run",
"very_great_work", "bad_run", "fair_run_decent")), class = c("spec_tbl_df",
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -4L), spec = structure(list(
cols = list(Name = structure(list(), class = c("collector_character",
"collector")), Adjusted_Name = structure(list(), class = c("collector_character",
"collector"))), default = structure(list(), class = c("collector_guess",
"collector")), skip = 1L), class = "col_spec"))

How to change column name according to another dataframe in R?

We can just remove the substring with rename_at

library(stringr)
libraryr(dplyr)
df1 %>%
table %>%
data.frame(.) %>%
rename_at(1:3, ~ str_remove(., "col"))

Or if it needs to be from 'df2'

df1 %>%
table %>%
data.frame(.) %>%
rename_at(1:3, ~ setNames(as.character(df2$altname), df2$name)[.])

Update

If all the column names in 'df1' are not in key/val columns of 'df2', an option is

df1 %>%
table %>%
data.frame(.) %>%
rename_at(1:3, ~ coalesce(setNames(as.character(df2$altname), df2$name)[.], .))

Or using base R

out <- df1 %>% table %>% data.frame(.)
names(out) <- sub("col", "", names(out))

if it needs to be based on a second dataset

name(out)[-4] <- df2$altname[match(names(out)[-4], df2$name)]

Or with substr

names(out) <- substring(names(out), 4)

Rename dataframe columns based on values in another dataframe in R

You can access values for column names for DF2 using $ or [[ operator on DF1. Since OP has mentioned that he doesn't even know the column names of DF1, an option could be as:

names(DF2) <- DF1[,1]
DF2
# M_D ABC123
#1 111 345
#2 456 999

#OR
names(DF2) <- DF1[[1]]

#OR
names(DF2) <- DF1$Name

Note: names(DF2) <- DF1[1] will not work as DF1[1] is still of type data.frame and names<- expects a vector.

Data:

DF2 <- read.table(text = 
"PQR LMN
111 345
456 999",
header = TRUE, stringsAsFactors = FALSE)

DF1 <- read.table(text =
"Name
M_D
ABC123",
header = TRUE, stringsAsFactors = FALSE)

Rename Columns with names from another data frame

Here's an example using the built-in mtcars data frame. We'll use the match function to find the indices of the columns names we want to replace and then replace them with new names.

# Copy of built-in data frame
mt = mtcars

head(mt,3)
                   mpg cyl disp  hp drat    wt  qsec vs am gear carb
Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
# Data frame with column name substitutions
dat = data.frame(old=c("mpg","am"), new=c("new.name1","new.name2"), stringsAsFactors=FALSE)

dat
  old       new
1 mpg new.name1
2 am new.name2

Use match to find the indices of the "old" names in the mt data frame:

match(dat[,"old"], names(mt))
[1] 1 9

Substitute "old" names with "new" names:

names(mt)[match(dat[,"old"], names(mt))] = dat[,"new"]

head(mt,3)
                  new.name1 cyl disp  hp drat    wt  qsec vs new.name2 gear carb
Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1

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!)



Related Topics



Leave a reply



Submit