Home > Net >  Replace only some NA values for selected rows and for only a column in R
Replace only some NA values for selected rows and for only a column in R

Time:06-14

I have a dataset which looks like this:

title                   |   comments        |   type     |  Asked
-----------------------------------------------------------------
Military guidelines     |   I like...       |   NA       |  TRUE
comments                |   I wish...       |   Question |  FALSE
comments                |   Das ist....     |   Question |  FALSE
comments                |   Es war...       |   Question |  FALSE
comments                |   Most of the...  |   NA       |  FALSE
comments                |   Can you clarify.|   NA       |  FALSE
comments                |   Be it the...    |   Answer   |  FALSE
comments                |   Best of five... |   Answer   |  FALSE
comments                |   Material...     |   Answer   |  FALSE
comments                |   Database on...  |   Answer   |  FALSE
Network guidelines      |   Government...   |   NA       |  TRUE
comments                |   Lack of...      |   NA       |  FALSE
comments                |   Aim the....     |   NA       |  FALSE
comments                |   Motive ...      |   Answer   |  FALSE
comments                |   Rules to su...  |   Answer   |  FALSE
Statutory measures      |   Warning...      |   NA       |  TRUE
comments                |   Lack of...      |   Question |  FALSE
comments                |   Aim the....     |   NA       |  FALSE
comments                |   Motive ...      |   Answer   |  FALSE
comments                |   Rules to su...  |   Answer   |  FALSE
Methods machines?       |   Topic 1...      |   NA       |  TRUE
comments                |   Surpass...      |   NA       |  FALSE

I want to replace NA with replies only where the type column has Answer and below to Question.

The NA of column type where Asked == TRUE should not be replaced.

Desired:

title                   |   comments        |   type     |  Asked
-----------------------------------------------------------------
Military guidelines     |   I like...       |   NA       |  TRUE
comments                |   I wish...       |   Question |  FALSE
comments                |   Das ist....     |   Question |  FALSE
comments                |   Es war...       |   Question |  FALSE
comments                |   Most of the...  |   Replies  |  FALSE
comments                |   Can you clarify.|   Replies  |  FALSE
comments                |   Be it the...    |   Answer   |  FALSE
comments                |   Best of five... |   Answer   |  FALSE
comments                |   Material...     |   Answer   |  FALSE
comments                |   Database on...  |   Answer   |  FALSE
Network guidelines      |   Government...   |   NA       |  TRUE
comments                |   Lack of...      |   Replies  |  FALSE
comments                |   Aim the....     |   Replies  |  FALSE
comments                |   Motive ...      |   Answer   |  FALSE
comments                |   Rules to su...  |   Answer   |  FALSE
Statutory measures      |   Warning...      |   NA       |  TRUE
comments                |   Lack of...      |   Question |  FALSE
comments                |   Aim the....     |   Replies  |  FALSE
comments                |   Motive ...      |   Answer   |  FALSE
comments                |   Rules to su...  |   Answer   |  FALSE
Methods machines?       |   Topic 1...      |   NA       |  TRUE
comments                |   Surpass...      |   Replies  |  FALSE

I would like to add a new column named replied based on this.

I tried something like:

df %>% 
  mutate(replied = if_else(!is.na(type)) AND Asked == TRUE, TRUE, FALSE)

But stuck at how to properly filter this out.

CodePudding user response:

df$type[!df$Asked & is.na(df$type)] <- "Replies" gets you to your desired table:

> type <-
    c(NA, rep("Question",3), NA, NA,  rep("Answer",4), rep(NA, 3), rep("Answer",2),
      NA, "Question", NA, rep("Answer",2), NA,NA)
> Asked <- c(
    T, rep(F, 9), T, rep(F, 4), T, rep(F, 4), T,F
  )
> df <- data.frame(title = 1:22, comments = 1:22, type, Asked)
> df$type[!df$Asked & is.na(df$type)] <- "Replies"
> df
   title comments     type Asked
1      1        1     <NA>  TRUE
2      2        2 Question FALSE
3      3        3 Question FALSE
4      4        4 Question FALSE
5      5        5  Replies FALSE
6      6        6  Replies FALSE
7      7        7   Answer FALSE
8      8        8   Answer FALSE
9      9        9   Answer FALSE
10    10       10   Answer FALSE
11    11       11     <NA>  TRUE
12    12       12  Replies FALSE
13    13       13  Replies FALSE
14    14       14   Answer FALSE
15    15       15   Answer FALSE
16    16       16     <NA>  TRUE
17    17       17 Question FALSE
18    18       18  Replies FALSE
19    19       19   Answer FALSE
20    20       20   Answer FALSE
21    21       21     <NA>  TRUE
22    22       22  Replies FALSE
  • Related