在R中为多个项目生成警告

时间:2020-12-30 22:59:30

I have a question about generating warnings for more than one items at a time in R. Please refer to the following dataframe and codes:

我有一个关于在R中一次为多个项目生成警告的问题。请参考以下数据框和代码:

Dataframe dat:

Dataframe dat:

  inputs var1 var2
A    1    a    1
B    2    b    3
B    3    b   NA
C    4    d   NA
C    5    e    4

if (any(duplicated(dat$inputs))==T){
  warning(paste("The following inputs: ", dat$inputs[duplicated(dat$inputs)],"is duplicated.",sep=""))
}

As you can see both B and C will be shown in the warning, like:

如您所见,警告中将显示B和C,例如:

Warning message:
The following inputs: B is duplicated.The following inputs: C is duplicated.

I'm okay with such warning message output, but it is not ideal. Is there a way to combine the two sentences and make it look like:

我没有这样的警告信息输出,但它并不理想。有没有办法将两个句子组合起来,使它看起来像:

Warning message:
The following inputs: B,C are duplicated.

Thanks a lot in advance for your attention and time.

非常感谢您的关注和时间。

Helene

海琳

1 个解决方案

#1


1  

I couldn't get your code to run, so I made up some/modified your code/data.

我无法运行您的代码,所以我编写了一些/修改了您的代码/数据。

dat = read.table(text = "
inputs var1 var2 var3
A    1    a    1
B    2    b    3
B    3    b   NA
C    4    d   NA
C    5    e    4", header = T)


if (any(b<-duplicated(dat$inputs))){
  if (length(c<-unique(dat$inputs[b]))>1) {warning(paste0("The following inputs: ", paste0(c, collapse=", "), " are duplicated."))} else
  {warning(paste0("The following input: ", paste0(c, collapse=", "), " is duplicated."))}
}


Warning message:
The following inputs: B, C are duplicated. 

Single duplicate

单个重复

dat = read.table(text = "
inputs var1 var2 var3
A    1    a    1
A    2    b    3
E    3    b   NA
C    4    d   NA
G    5    e    4", header = T)

Warning message:
The following input: A is duplicated. 

#1


1  

I couldn't get your code to run, so I made up some/modified your code/data.

我无法运行您的代码,所以我编写了一些/修改了您的代码/数据。

dat = read.table(text = "
inputs var1 var2 var3
A    1    a    1
B    2    b    3
B    3    b   NA
C    4    d   NA
C    5    e    4", header = T)


if (any(b<-duplicated(dat$inputs))){
  if (length(c<-unique(dat$inputs[b]))>1) {warning(paste0("The following inputs: ", paste0(c, collapse=", "), " are duplicated."))} else
  {warning(paste0("The following input: ", paste0(c, collapse=", "), " is duplicated."))}
}


Warning message:
The following inputs: B, C are duplicated. 

Single duplicate

单个重复

dat = read.table(text = "
inputs var1 var2 var3
A    1    a    1
A    2    b    3
E    3    b   NA
C    4    d   NA
G    5    e    4", header = T)

Warning message:
The following input: A is duplicated. 

相关文章