替换包含字符的数据框中的每个字符串

时间:2022-08-19 19:35:43

In the test dataframe below, I am attempting to change every string in the dataframe containing "NA" to "" (so as to make NAs blank).

在下面的测试数据框中,我试图将包含“NA”的数据帧中的每个字符串更改为“”(以便使NAs为空)。

dat <- as.data.frame(matrix(ncol=2, nrow=2))
dat$V1 <- c("  NA", "foo")
dat$V2 <- c("bar", "NA   ")

dat
   V1   V2
1  NA  bar
2 foo NA 

However, the following command returns a completely blank dataframe, as if all strings contained "NA". Why does this happen and what would be the correct solution?

但是,以下命令返回一个完全空白的数据帧,就好像所有字符串都包含“NA”一样。为什么会发生这种情况?什么是正确的解决方案?

value <- "NA"

dat[grepl(value, dat)] <- ""

4 个解决方案

#1


1  

Just using gsub

只是使用gsub

value <- "NA" 

for (i in 1:ncol(dat)) {
  dat[,i] <- gsub(value, "", dat[,i])  
}
dat

#2


1  

dat <- lapply(dat, function(x) {gsub("NA", "", x)})
dat <- data.frame(dat)

#3


0  

library(data.table)
setDT(dat)

for(j in seq_along(dat)){
  set(dat, i = which(dat[[j]] %like% "NA"), j = j, value = "")
}
      V1  V2
# 1:     bar
# 2: foo  

#4


0  

Maybe in your case you are better off with a matrix.

也许在你的情况下你最好用矩阵。

datm <- as.matrix(dat)

Now your proposed solution works:

现在您提出的解决方案有效

datm[grepl(value, datm)] <- ""

or using gsub:

或使用gsub:

datm = gsub("\\s*NA\\s*", "",datm)

You can convert it to a dataframe after data cleansing.

您可以在数据清理后将其转换为数据帧。

#1


1  

Just using gsub

只是使用gsub

value <- "NA" 

for (i in 1:ncol(dat)) {
  dat[,i] <- gsub(value, "", dat[,i])  
}
dat

#2


1  

dat <- lapply(dat, function(x) {gsub("NA", "", x)})
dat <- data.frame(dat)

#3


0  

library(data.table)
setDT(dat)

for(j in seq_along(dat)){
  set(dat, i = which(dat[[j]] %like% "NA"), j = j, value = "")
}
      V1  V2
# 1:     bar
# 2: foo  

#4


0  

Maybe in your case you are better off with a matrix.

也许在你的情况下你最好用矩阵。

datm <- as.matrix(dat)

Now your proposed solution works:

现在您提出的解决方案有效

datm[grepl(value, datm)] <- ""

or using gsub:

或使用gsub:

datm = gsub("\\s*NA\\s*", "",datm)

You can convert it to a dataframe after data cleansing.

您可以在数据清理后将其转换为数据帧。