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

前端 未结 6 537
说谎
说谎 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:58

    Edit 2: See @flodel's answer. Much better.

    Try:

    # assuming SFI is your data.frame
    as.matrix(sapply(SFI, as.numeric))  
    

    Edit: or as @ CarlWitthoft suggested in the comments:

    matrix(as.numeric(unlist(SFI)),nrow=nrow(SFI))
    

提交回复
热议问题