我有一个关于在R中一次为多个项目生成警告的问题。请参阅以下数据框和代码:
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=""))
}
如您所见,警告中将显示B和C,例如:
Warning message:
The following inputs: B is duplicated.The following inputs: C is duplicated.
我可以输出这样的警告信息,但这并不理想。有没有办法将两个句子组合起来并使它看起来像:
Warning message:
The following inputs: B,C are duplicated.
提前感谢您的关注和时间。
海伦
答案 0 :(得分:1)
我无法运行您的代码,因此我编写了一些/修改了您的代码/数据。
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.
单一重复
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.