1

I think I am very close to the solution. But unfortunately I don't get it.

x1kb <- c(1:10, NA, NA, 5,6,7,8)
y1 <- rep(c(NA,777),each=8)
x2kb <- c(1:10, NA, NA, 5,6,7,8)
y2 <- rep(c(NA,777),each=8)
data1<- data.frame(x1kb, y1, x2kb, y2)

This is how the dataframe looks like:

   x1kb  y1 x2kb  y2
1     1  NA    1  NA
2     2  NA    2  NA
3     3  NA    3  NA
4     4  NA    4  NA
5     5  NA    5  NA
6     6  NA    6  NA
7     7  NA    7  NA
8     8  NA    8  NA
9     9 777    9 777
10   10 777   10 777
11   NA 777   NA 777
12   NA 777   NA 777
13    5 777    5 777
14    6 777    6 777
15    7 777    7 777
16    8 777    8 777

I tried it with dplyr. But I always get an error.

library(dplyr)
#replace NA with 0 if colname includes "kb"
data1[is.na(data1)] <- 0 %>% select(data1, contains("kb"))

My desired output:

   x1kb  y1 x2kb  y2
1     1  NA    1  NA
2     2  NA    2  NA
3     3  NA    3  NA
4     4  NA    4  NA
5     5  NA    5  NA
6     6  NA    6  NA
7     7  NA    7  NA
8     8  NA    8  NA
9     9 777    9 777
10   10 777   10 777
11    0 777    0 777
12    0 777    0 777
13    5 777    5 777
14    6 777    6 777
15    7 777    7 777
16    8 777    8 777
SDahm
  • 474
  • 2
  • 9
  • 21
  • @akrun: I think it is a different question. Your link only clears half of my question and not the important thing which is to replace NAs only in some columns but not in all. – SDahm Feb 04 '16 at 14:43
  • Okay, I reopened it. – akrun Feb 04 '16 at 16:55

2 Answers2

4

Here is a dplyr solution with mutate_each. You can use the same selectors you use in select() to restrict it to the columns you want. Here I use ends_with.

data1 <- mutate_each(data1, funs=funs(ifelse(is.na(.),0,.)), ends_with("kb"))

Edit: recent version of dplyr have soft deprecated *_each() functions. Using across(). See this answer for an example. Here, the new answer would be:

data1 <- mutate(data1, across(ends_with("kb"), ~ifelse(is.na(.x),0,.x)))
scoa
  • 19,359
  • 5
  • 65
  • 80
  • Note that if the columns of interest are factors, the factor values will be replaced with their numerical indices – knowah May 10 '16 at 13:19
  • 1
    See: https://stackoverflow.com/questions/64667557/how-to-replace-nas-in-multiple-columns-with-dplyr for an answer using updated **dplyr** code. – jtr13 Nov 03 '20 at 17:52
  • thank you for pointing that out, I updated the answer – scoa Nov 04 '20 at 18:59
1

Try

data1$x1kb[is.na(data1$x1kb)] <- 0
data1$x2kb[is.na(data1$x2kb)] <- 0
RHertel
  • 23,412
  • 5
  • 38
  • 64