0

I have a data.frame as the following :

myPhoneHTML

     X1            X2    X3     X4     X5 X6       X7

  1 Brand         Model Price Screen Weight GB     Date

  2 Apple       iPhone7 24500    4.7    138 32 20160916

  3   HTC         OneM8 21900      5    160 16 20140328

  4   HTC         OneS9  9990      5    158 32 20160617

  5  ASUS ZenFoneDeluxe  8990    5.5    170 16 20150827

  6  ASUS   ZenFoneZoom 15990    5.5    185 64 20151201

How do i take Brand~Data as factor instead of x1~x7?

like this :

    Brand         Model Price Screen Weight GB     Date

1   Apple       iPhone7 24500    4.7    138 32 20160916

2   HTC         OneM8 21900    5.0    160 16 20140328

3   HTC         OneS9  9990    5.0    158 32 20160617

4  ASUS ZenFoneDeluxe  8990    5.5    170 16 20150827

5  ASUS   ZenFoneZoom 15990    5.5    185 64 20151201

(There's no x1~x7 but Brand~Data) Thanks!

連振宇
  • 31
  • 4

1 Answers1

4

We can assign the first row as the column names and then remove the first row

colnames(myPhoneHTML) <- myPhoneHTML[1,]
myPhoneHTML <- myPhoneHTML[-1,]
myPhoneHTML[] <- lapply(myPhoneHTML, function(x) type.convert(as.character(x), as.is = TRUE))

It is not clear how the data is being read. If we use read.csv/read.table, specify the header = TRUE to read the first row as the column name

dat <- read.csv("file.csv", header = TRUE, stringsAsFactors = FALSE)
akrun
  • 874,273
  • 37
  • 540
  • 662