10

In my data frame, I want to replace certain blank cells and cells with values with NA. But the cells I want to replace with NAs has nothing to do with the value that cell stores, but with the combination of row and column it is stored in.

Here's a sample data frame DF:

  Fruits   Price   Weight   Number of pieces

  Apples      20      2          10
  Oranges     15      4          16
  Pineapple   40      8           6
  Avocado     60      5          20

I want to replace Pineapple'e weight to NA and Orange's number of pieces to NA.

DF$Weight[3] <- NA
DF$`Number of pieces`[2] <- NA  

This replaces any value that's stored in that position and that may change. I want to use specific row and column names to do this replacement so the position of value becomes irrelevant.

Output:

 Fruits   Price   Weight   Number of pieces

  Apples      20      2          10
  Oranges     15      4          NA
  Pineapple   40      NA           6
  Avocado     60      5          20

But if order of the table is changed, this would replace wrong values with NA.

How should I do this?

2
  • How many rows do you intend to replace? Commented Feb 10, 2019 at 10:33
  • I have 37 rows and 26 columns in my dataset. I intend to replace at least 10 cells in different row/column combination with NA. Commented Feb 10, 2019 at 10:37

3 Answers 3

17

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.

Sign up to request clarification or add additional context in comments.

Comments

4
library(dplyr)
df %>% 
  mutate(Weight=ifelse(Fruits=="Pineapple",NA,Weight),
         Number=ifelse(Fruits=="Oranges",NA,Number))#use Number of Pieces

Result: Number of pieces was truncated to Number due to reading data.

     Fruits Price Weight Number
1    Apples    20      2     10
2   Oranges    15      4     NA
3 Pineapple    40     NA      6
4   Avocado    60      5     20

Comments

3

Here is a way using function is.na<-.

is.na(DF$Weight) <- DF$Fruits == "Pineapple"
is.na(DF$`Number of pieces`) <- DF$Fruits == "Oranges"

DF
#     Fruits Price Weight Number of pieces
#1    Apples    20      2               10
#2   Oranges    15      4               NA
#3 Pineapple    40     NA                6
#4   Avocado    60      5               20

Data in dput format.

DF <-
structure(list(Fruits = structure(c(1L, 3L, 4L, 2L), 
.Label = c("Apples", "Avocado", "Oranges", "Pineapple"), 
class = "factor"), Price = c(20L, 15L, 40L, 60L), 
Weight = c(2L, 4L, 8L, 5L), `Number of pieces` = c(10L, 
16L, 6L, 20L)), class = "data.frame", row.names = c(NA, -4L))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.