-1

Whats wrong in the input codes, where and why the numbers 5109 2 5 5.1 5.2 come to header, i want to id row at 4509 be the header.

data[1:3,1:5]

         ID   X1  X2 X3 X4
1    02003KH1 -9  1  2  1
2 02003KH1005 -9  0  1  2
3 02003KH1021 -9  1  1  1

> data[4507:4509,1:5]
           ID    X1 X2 X3 X4
4507 2010XX2178  -9 0 2 2
4508 2010XX2228  -9 0 2 2
4509 id        G_r8 G_r9 G_r5 G_r80

> names(data) <- data[data$ID=='id',]

> data[1:3,1:5]
    5109       2 5 5.1 5.2
1 02003KH1    -9 1   2   1
2 02003KH1005 -9 0   1   2
3 02003KH1021 -9 1   1   1

1 Answers1

0

You'll need to remove the factor levels from your data frame see similar posts on the subject (Convert data.frame columns from factors to characters, How to change factor labels into string in a data frame). Here's a short example based on the supplied data for answering the question.

dat = data.frame(ID=c('0200','02003','020034','id','020039'),
           X1=c(-9,-9,-9,'G1',-9),
           X2=c(1,0,1,'G2',0), stringsAsFactors = FALSE)
names(dat) <- dat[dat$ID=='id',]) 
dat <- dat[which(dat$id!='id'),]  # remove the 'id' row
sempervent
  • 833
  • 2
  • 11
  • 23
  • its Good, but i dont know whats wrong in my dataset, as you show i should set the id row to be header. my id row in dataset is 4509 number of row. i did the names(data)<-data[4509,] but instead the id row the some unusual numbers go to head 5109 2 5 5.1 5.2 i dont know where these numbers come from! – Agrii Enginee Jun 05 '17 at 21:47
  • Did you convert it to a character first? You'll need to remove the factor levels to get it to work. Try adding `stringsAsFactors=FALSE` when you import the `data.frame`. You can also specify the number of the row (e.g. `names(dat) <- dat[32,]`). – sempervent Jun 06 '17 at 00:54