0

My column names have hyphens ("-"). When reading as table, the column names have (".") in place of ("-"). I want to retain the column names as-is.

df = read.table("meth_clin_kipan_pathanalysis.txt", header=T, sep="\t", row.names=1)

Raw:

TCGA-2K-A9WE-01A TCGA-2Z-A9J1-01A
First row
Second row

Current output:

TCGA.2K.A9WE.01A TCGA.2Z.A9J1.01A
First row
Second row

Desired output (same as raw, i.e., as-is):

TCGA-2K-A9WE-01A TCGA-2Z-A9J1-01A
First row
Second row
melolilili
  • 199
  • 1
  • 3
  • 11

2 Answers2

2

Simply use check.names=FALSE argument of read.table to preserve original column names that have special characters and spaces.

Parfait
  • 104,375
  • 17
  • 94
  • 125
0

This could work

library(dplyr)
df %>%
  rename_with(~stringr::str_replace_all(., '\\.', '-'), everything())
Julian
  • 6,586
  • 2
  • 9
  • 33