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)'
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))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With