0

I have a datatset like this:

ID   A   B   C
1   1    0   0
1   NA   0   0
2   0    NA  NA
2   1    NA  0

reproducible with:

df <- data.frame("ID"=c(rep(1,2),rep(2,2)),"A"=c(1,NA,0,1),"B"=c(rep(0,2),rep(NA,2)),"C"=c(rep(0,2),NA,0))

How can I turn NA into 0 in column A and B without repeting every time:

df$A[is.na(df$A)] <- 0
df$B[is.na(df$B)] <- 0

with a more concise code?

jeff
  • 323
  • 1
  • 7

3 Answers3

1

If you have only two columns I would use your way or:

df[,c("A","B")][is.na(df[,c("A","B")])] <- 0

if there are many columns to change maybe:

i <- c("A","B")
df[,i][is.na(df[,i])] <- 0
df
#  ID A B  C
#1  1 1 0  0
#2  1 0 0  0
#3  2 0 0 NA
#4  2 1 0  0
GKi
  • 37,245
  • 2
  • 26
  • 48
1

With tidyr you could use the following solution:

library(tidyr)

df %>%
  replace_na(list(A = 0, B = 0))

In the list argument you can specify with what the NA value should be replaced.

Nico
  • 463
  • 5
  • 11
0

Update: As suggested by Darren

colList<-c("A","B")
df %>% mutate_at(colList, function(x) is.na(x)<-0 )

Using dplyr:

colList<-c("A","B")
df %>% mutate_if(names(df) %in% colList, function(x) is.na(x)<-0 )
  ID A B  C
1  1 0 0  0
2  1 0 0  0
3  2 0 0 NA
4  2 0 0  0
rj-nirbhay
  • 649
  • 7
  • 23