Data frame typecasting entire column to character from numeric

后端 未结 2 933
梦毁少年i
梦毁少年i 2021-01-23 12:49

Suppose I have a data.frame that\'s completely numeric. If I make one entry of the first column a character (for example), then the entire

2条回答
  •  -上瘾入骨i
    2021-01-23 13:17

    Short answer is you cannot.
    As was mentioned in the comments, in a data frame, all elements of a column must have the same mode.

    If you would like to specifically find the values that are "number like" you can use the following (where vec here would be, say, a data frame column)

      vec[!is.na(as.numeric((vec)))]
    

    You can then convert these, but unfortunately you cannot put the converted values back into the same column. As as you do, they will be coerced back to character



    As for a function that can convert the whole dataframe to numeric (realizing that isolating specific entries as exceptions is not possible), you can use sapply

      sapply(dataFrameName, as.numeric)
    

提交回复
热议问题