Right way to convert data.frame to a numeric matrix, when df also contains strings?

前端 未结 6 643
说谎
说谎 2020-12-22 19:34

I have a data frame taken from a .csv-file which contains numeric and character values. I want to convert this data frame into a matrix. All containing information is number

6条回答
  •  时光取名叫无心
    2020-12-22 19:48

    Another way of doing it is by using the read.table() argument colClasses to specify the column type by making colClasses=c(*column class types*). If there are 6 columns whose members you want as numeric, you need to repeat the character string "numeric" six times separated by commas, importing the data frame, and as.matrix() the data frame. P.S. looks like you have headers, so I put header=T.

    as.matrix(read.table(SFI.matrix,header=T,
    colClasses=c("numeric","numeric","numeric","numeric","numeric","numeric"),
    sep=","))
    

提交回复
热议问题