Convert a data frame column from character to numeric

I have a data frame that I create as such:

> yyz <- data.frame(a = c("1","2","n/a"), b = c(1,2,"n/a")) > apply(yyz, 2, class) ab "character" "character" 

I am trying to convert the last column to a numeric one while keeping the first column as a character. I tried this:

 > yyz$b <- as.numeric(as.character(yyz$b)) > yyz ab 1 1 2 2 n/a NA 

But when I run the apply class, it shows me that they are both character classes.

 > apply(yyz, 2, class) ab "character" "character" 

Am I setting the data frame incorrectly? Or is it the way R interprets a data frame?

+7
r
source share
1 answer

If we need only one numeric column

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

But if all the columns need to be changed to numeric , use lapply to loop lapply columns and convert to numeric , first converting it to the character class, since the columns were factor .

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

Both columns in the OP column are factor due to the string "n/a" . This could be easily avoided when reading the file with na.strings = "n/a" in read.table/read.csv , or if we use data.frame , we can have character columns with stringsAsFactors=FALSE (by default this stringsAsFactors=TRUE )


Regarding the use of apply , it converts the dataset to matrix , and matrix can contain only one class. To check the class we need

 lapply(yyz, class) 

or

 sapply(yyz, class) 

Or check

 str(yyz) 
+14
source share

All Articles