【发布时间】:2020-05-20 05:20:03
【问题描述】:
我在下面有一个数据框。
asd <- data.frame(a=c(1:10),b=c(11,20),c=(21:30),d=c(31:40),e=c(41,50),f=c(51,60))
以下是包含 asd 列名的 2 个向量
Column_list1 <- c("a","b","d")
Column_list2 <- c("c","e","f")
以下是列的强制列表
mandatory_column <- c("a","b","d","e")
现在实际上我的问题是当我尝试运行asd[Column_list1] 时,结果应该会抛出一条消息。因为必填列应该是c("a","b","d","e"),并且由于Column_list1 没有“e”,所以我需要打印消息“请同时包含“e”列”。这有可能实现吗?
预期输出
asd[Column_list1]
Please include column "e" as well
asd[mandatory_column]
a b d e
1 1 11 31 41
2 2 20 32 50
3 3 11 33 41
4 4 20 34 50
5 5 11 35 41
6 6 20 36 50
7 7 11 37 41
8 8 20 38 50
9 9 11 39 41
10 10 20 40 50
【问题讨论】:
-
stopifnot( all(mandatory_column %in% colnames(asd)) )? -
你是想写一个函数还是什么?您希望在哪里进行此项检查?
-
我正在编写一个闪亮的应用程序。因此,如果我运行 asd[column_list1],它应该会显示一条消息“请同时选择 e 列”
-
@r2evans ,我尝试了
stopifnot(all(mandatory_column %in% Column_list1))并得到了这个错误Error: all(mandatory_column %in% Column_list1) is not TRUE。而不是这个错误,我可以有“请同时选择 e 列” -
使用
asd[union(mandatory_column, Column_list1)]之类的不是更方便吗?
标签: r