1

I have one column in a table where I need to change 0 to 1. I undestand that basically it should be something like

 if(0 %in% COLUMN){then add 1} 

but I'm not sure and can't find the way how to make it correctly so the R would add 1 to each 0 in one exact column.

Ally
  • 23
  • 3

2 Answers2

4

We don't need a if/else loop. It can be directly done with vectorized option

df1$COLUMN[df1$COLUMN == 0] <- 1

Or another option is

df1$COLUMN <- (!df1$COLUMN) + df1$COLUMN

data

df1 <- data.frame(COLUMN = c(5, 0, 3, 2, 0))
akrun
  • 874,273
  • 37
  • 540
  • 662
2

Another base R option is to use ifelse

COLUMN <- ifelse(COLUMN==0,1,COLUMN)
ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81