Home > Blockchain >  How to rename header line in R dataframe?
How to rename header line in R dataframe?

Time:06-24

I have a dataframe that looks like this in R:

dataframe <- read.table(file="~/Desktop/prs_profile.txt", header = T, sep="\t")

     FID......IID..PHENO....CNT...CNT2....SCORE
1 1032   468768     -9     74      9 0.00864209
2  1405   468769     -9     92      7 0.0084487
3  1564   468770     -9     92     10 0.0100944
4 1610   468771     -9     92     10 0.00902744
5  998   468774     -9     86      6 0.00773592
6  975   468775     -9     90      8 0.00729964

Each header word should correspond to a column however when I print the header, it does not exist as separate column names e.g.

> colnames(dataframe)

[1] "FID......IID..PHENO....CNT...CNT2....SCORE"

I want to replace the header line so I have tried the following but get an error:

names(dataframe) <- c("FID", "IID", "PHENO", "CNT", "CNT2", "SCORE")

Error in names(dataframe) <- c("FID", "IID", "PHENO", "CNT", "CNT2",  : 
  'names' attribute [6] must be the same length as the vector [1]

I have read other similar posts here on stackoverflow but this does not apply to my case e.g.

> setnames(OedemaG2_PRS, c("FID", "IID", "PHENO", "CNT", "CNT2", "SCORE"))
Error in setnames(OedemaG2_PRS, c("FID", "IID", "PHENO", "CNT", "CNT2",  : 
  Can't assign 6 names to a 1 column data.table

CodePudding user response:

You have a dataframe with only 1 column. After reading it, try applying a "data.frame" function in it and see if the number of columns become 6.

dataframe <- data.frame(read.table(file="~/Desktop/prs_profile.txt", header = T, sep="\t"))

Then, you can try to pass the name(dataframe) as you did. If it still does not work, the problem is with your database. You have to read it in such a way that 6 columns are found.

CodePudding user response:

For change columns use colnames()

colnames(dataframe) <- c("FID", "IID", "PHENO", "CNT", "CNT2", "SCORE")

If you want to change rows, you can use rownames() like colnames().

  • Related