How to Replace Certain Values in a Specific Rows and Columns with Na in R

How to replace certain values in a specific rows and columns with NA in R?

Since your data structure is 2 dimensional, you can find the indices of the rows containing a specific value first and then use this information.

which(DF$Fruits == "Pineapple")
[1] 3
DF$Weight[which(DF$Fruits == "Pineapple")] <- NA

You should be aware of that which will return a vector, so if you have multiple fruits called "Pineapple" then the previous command will return all indices of them.

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

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

How to change NA value in a specific row and column?

Try something like this. Add in a rowID and then create a lookup data frame with your replacements. Then you can just left_join() in the new values.

The lookup_df replaces your "if this company and this row then new value" logic.

library(dplyr)

df <- tibble(companyID = c(rep(1, 5), rep(2, 6)), value = NA_character_)

lookup_df <- tibble(companyID = c(1, 2), rowID = c(3, 4), valueNew = c("F", "D"))

df %>%
group_by(companyID) %>%
mutate(rowID = row_number()) %>%
left_join(lookup_df, by = c("companyID", "rowID")) %>%
mutate(value = coalesce(value, valueNew)) %>%
select(companyID, value)

result:

# A tibble: 11 x 2
# Groups: companyID [2]
companyID value
<dbl> <chr>
1 1 NA
2 1 NA
3 1 F
4 1 NA
5 1 NA
6 2 NA
7 2 NA
8 2 NA
9 2 D
10 2 NA
11 2 NA

Replacing certain NA values with a specific value

We can use ifelse (or case_when) with your conditions:

your_data %>%
mutate(Cq = ifelse(is.na(Cq) & Dilution %in% c("1", "1:1"), 31, Cq))
# Sample Dilution Cq
# 1 1 1 31
# 2 1 1:1 31
# 3 1 1:5 27
# 4 2 1 13
# 5 2 1:1 14
# 6 2 1:5 15

Using this data:

your_data = read.table(text = 'Sample   Dilution    Cq
1 1 NA
1 1:1 NA
1 1:5 27
2 1 13
2 1:1 14
2 1:5 15', header = T)

replace values with NA in several columns

We may do this in two steps - loop across the columns that have 'VAR' followed by digits (\\d+) in column names, replace the values where the first two characters are not AA or DD to NA, then replace the corresponding DATE column to NA based on the NA in the 'VAR1', 'VAR2' columns

library(dplyr)
library(stringr)
DF %>%
mutate(across(matches("^VAR\\d+$"),
~ replace(., !substr(., 1, 2) %in% c("AA", "DD"), NA)),
across(ends_with("DATE"),
~ replace(., is.na(get(str_remove(cur_column(), "DATE"))), NA)))

-output

# A tibble: 5 × 5
ID VAR1 VAR1DATE VAR2 VAR2DATE
<int> <chr> <chr> <chr> <chr>
1 1 AABB 2001-01-01 <NA> <NA>
2 2 AACC 2001-01-02 AACC 2001-01-02
3 3 <NA> <NA> DDCC 2001-01-03
4 4 DDAA 2001-01-04 <NA> <NA>
5 5 <NA> <NA> <NA> <NA>


Related Topics



Leave a reply



Submit