简体   繁体   English

如何用R中的NA替换特定行和列中的某些值?

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

In my data frame, I want to replace certain blank cells and cells with values with NA. 在我的数据框中,我想用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. 但是我要替换为NA的单元格与单元格存储的值无关,但是与行和列的组合存储在其中。

Here's a sample data frame DF: 这是一个示例数据帧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. 我想将Pineapple'e的重量替换为NA,将Orange的件数替换为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. 但是,如果更改表的顺序,则会用NA替换错误的值。

How should I do this? 我应该怎么做?

Here is a way using function is.na<- . 这是使用函数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. dput格式的数据。

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))

Since you data structure is 2 dimensional you can find the indices of the rows containing a specific value first and 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. 你应该知道的是which会返回一个矢量,所以如果你有多个水果被称为“菠萝”,那么前面的命令将返回他们的各项指标。

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. 结果:由于读取数据,件数被截断为Number。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM