Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ifelse returning only numeric value

Tags:

r

I am a new R user and have just started working with dataframes. I am trying to create a new column within a dataframe (using the code below). The problem is the new column created contains numeric values, yet all the columns used in the code are non-numeric

I have tried looking for answer on-line but cannot find an answer

dataframe$newcol <- ifelse(dataframe$colA == "London", dataframe$colA, dataframe$colB)'
like image 771
nir020 Avatar asked Dec 30 '25 18:12

nir020


1 Answers

R defaults alot of character columns to factors, which can be a little tricky.

You can look at the class of variables like this

sapply( dataframe, class )

or

str( dataframe )

You can convert multiple columns like this:

dataframe[ , c("colA" ,"colB") ] <- sapply( dataframe[ , c("colA" ,"colB") ] , as.character )

you can convert one column at a time like this

dataframe$colA <- as.character( dataframe$colA )

if you are converting numeric cols do it like this

dataframe$colX <- as.numeric( as.character( dataframe$colX ))

Your code should work now - note that I changed == to %in%

dataframe$newcol <- ifelse(dataframe$colA %in% "London", dataframe$colA, dataframe$colB)

you can save yourself typing by using transform here

dataframe <- transform( dataframe , newcol = ifelse( colA %in% "London", colA, colB))
like image 175
MatthewR Avatar answered Jan 01 '26 12:01

MatthewR



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!