替换R中数据帧的子矩阵中的值

时间:2022-06-14 16:21:55

I have a dataframe, say x, and I would like to replace the 0 values with NA, for columns say c("A", "B", "C", "D"), on rows 1:10. Is there an efficient/compact way of doing it?

我有一个数据帧,比如说x,我想用NA替换0值,对于列c(“A”,“B”,“C”,“D”),行1:10。这样做有效/紧凑吗?

1 个解决方案

#1


1  

Try: If you want to replace NA's for the whole dataset:

尝试:如果要替换整个数据集的NA:

set.seed(41)
d1 <- as.data.frame( matrix(sample(0:5, 4*10, replace=TRUE), dimnames=list(NULL, LETTERS[1:4]), ncol=4))
 d1[!d1] <- NA
 d1

If you have more columns in your dataset and want to replace only for a subset of columns:

如果数据集中有更多列,并且只想替换列的子集:

set.seed(41)
d2 <- as.data.frame( matrix(sample(0:5, 8*10, replace=TRUE), dimnames=list(NULL, LETTERS[1:8]), ncol=8))
d2[,LETTERS[1:4]][!d2[,LETTERS[1:4]]] <- NA
d2
 #   A  B  C D E F G H
#1   1  4 NA 3 1 5 2 1
#2   5  4  4 3 5 4 5 0
#3   3  4  1 4 5 1 0 4
#4  NA  1  4 5 3 5 1 1
#5   5 NA  5 4 0 0 4 5
#6   5  4  3 4 2 0 4 5
#7   5  5  2 3 2 1 3 4
#8   3 NA  1 1 5 0 2 0
#9   4  5  2 5 3 0 0 1
#10  4 NA  2 5 4 1 1 0

If it is for a subset of 5 rows and 4 columns

如果它是5行4列的子集

d2[1:5, LETTERS[1:4]][!d2[1:5, LETTERS[1:4]]] <- NA
 d2
 #    A  B  C D E F G H
 #1   1  4 NA 3 1 5 2 1
 #2   5  4  4 3 5 4 5 0
 #3   3  4  1 4 5 1 0 4
 #4  NA  1  4 5 3 5 1 1
 #5   5 NA  5 4 0 0 4 5
 #6   5  4  3 4 2 0 4 5
 #7   5  5  2 3 2 1 3 4
 #8   3  0  1 1 5 0 2 0
 #9   4  5  2 5 3 0 0 1
 #10  4  0  2 5 4 1 1 0

You can check the difference in results for the above two cases

您可以检查上述两种情况的结果差异

#1


1  

Try: If you want to replace NA's for the whole dataset:

尝试:如果要替换整个数据集的NA:

set.seed(41)
d1 <- as.data.frame( matrix(sample(0:5, 4*10, replace=TRUE), dimnames=list(NULL, LETTERS[1:4]), ncol=4))
 d1[!d1] <- NA
 d1

If you have more columns in your dataset and want to replace only for a subset of columns:

如果数据集中有更多列,并且只想替换列的子集:

set.seed(41)
d2 <- as.data.frame( matrix(sample(0:5, 8*10, replace=TRUE), dimnames=list(NULL, LETTERS[1:8]), ncol=8))
d2[,LETTERS[1:4]][!d2[,LETTERS[1:4]]] <- NA
d2
 #   A  B  C D E F G H
#1   1  4 NA 3 1 5 2 1
#2   5  4  4 3 5 4 5 0
#3   3  4  1 4 5 1 0 4
#4  NA  1  4 5 3 5 1 1
#5   5 NA  5 4 0 0 4 5
#6   5  4  3 4 2 0 4 5
#7   5  5  2 3 2 1 3 4
#8   3 NA  1 1 5 0 2 0
#9   4  5  2 5 3 0 0 1
#10  4 NA  2 5 4 1 1 0

If it is for a subset of 5 rows and 4 columns

如果它是5行4列的子集

d2[1:5, LETTERS[1:4]][!d2[1:5, LETTERS[1:4]]] <- NA
 d2
 #    A  B  C D E F G H
 #1   1  4 NA 3 1 5 2 1
 #2   5  4  4 3 5 4 5 0
 #3   3  4  1 4 5 1 0 4
 #4  NA  1  4 5 3 5 1 1
 #5   5 NA  5 4 0 0 4 5
 #6   5  4  3 4 2 0 4 5
 #7   5  5  2 3 2 1 3 4
 #8   3  0  1 1 5 0 2 0
 #9   4  5  2 5 3 0 0 1
 #10  4  0  2 5 4 1 1 0

You can check the difference in results for the above two cases

您可以检查上述两种情况的结果差异