Converting data frame column from character to numeric

后端 未结 1 1413
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-04 14:31

I have a data frame that I construct as such:

> yyz <- data.frame(a = c(\"1\",\"2\",\"n/a\"), b = c(1,2,\"n/a\"))

> apply(yyz, 2, class)
      a            


        
相关标签:
1条回答
  • 2020-12-04 15:17

    If we need only one column to be numeric

    yyz$b <- as.numeric(as.character(yyz$b))
    

    But, if all the columns needs to changed to numeric, use lapply to loop over the columns and convert to numeric by first converting it to character class as the columns were factor.

    yyz[] <- lapply(yyz, function(x) as.numeric(as.character(x)))
    

    Both the columns in the OP's post are factor because of the string "n/a". This could be easily avoided while reading the file using na.strings = "n/a" in the read.table/read.csv or if we are using data.frame, we can have character columns with stringsAsFactors=FALSE (the default is stringsAsFactors=TRUE)


    Regarding the usage of apply, it converts the dataset to matrix and matrix can hold only a single class. To check the class, we need

    lapply(yyz, class)
    

    Or

    sapply(yyz, class)
    

    Or check

    str(yyz)
    
    0 讨论(0)
提交回复
热议问题