R Change NA

My data looks like this: http://imgur.com/8KgvWvP

I want to change the values NAto a different value for each column. For example, in the column containing NA, Singleand Dual, I want to change everything NAto 'Single'.

I tried this code:

data_price$nbrSims <- ifelse(is.na(data_price$nbrSims), 'Single', data_price$nbrSims)

But then my data looks like this: Dualbecame 2and Single 1. http://imgur.com/TC1bIgw

How to change values NAwithout changing other values? Thanks in advance!

+4
source share
3 answers

, 1 2 ifelse, factor. character,

 data_price$nbrSims <- as.character(data_price$nbrSims)
 data_price$nbrSims <- ifelse(is.na(data_price$nbrSims), 
             'Single', data_price$nbrSims)
+5

(, NA "Single"):

data_price$nbrSims <- as.character(data_price$nbrSims)
data_price$nbrSims[is.na(data_price$nbrSims)] <- "Single"
+3

To be clear, Martha’s answer is right.

You can also change all Na values ​​using this

data_price[is.na(data_price)]<-"Something"
0
source

Source: https://habr.com/ru/post/1623737/


All Articles