我有几十份调查数据档案.每一列都有几列数字数据,然后是几列字符数据.我需要根据两个都必须满足的标准,动态地从每个列中删除一列:

  • 列是数字,并且...
  • 该列包含单词"all".

我不能简单地取消 Select 包含单词"all"的任何/所有列,因为我必须保留的其中一个字符列的标题中有"all".

我不能简单地按名称或职位取消 Select 它,因为不同的文件在职位或名称方面不一致.它们唯一的共同点是,这一栏是数字and,标题中有"总体".此外,并不是所有的文件都有这样的列,这就是为什么我try 动态地这样做的原因.

下面是一个这样的文件的非常简化的示例,它显示在数据帧中:

#### reproducible example ####

columns <- c("rating A", "rating B", "Student Overall Rating", 
              "feedback 1", "feedback 2", "Student Overall Feedback")
c1 <- c(4, 4, 3)
c2 <- c(5, 4, 4)
c3 <- c(4.5, 4, 3.5)
c4 <- c("blah", "blah", "blah")
c5 <- c("blah", "blah", "blah")
c6 <- c("blahblah", "blahblah", "blahblah")

df <- as.data.frame(cbind(c1, c2, c3, c4, c5, c6))
names(df) <- columns
df$`rating A` <- as.numeric(df$`rating A`)
df$`rating B` <- as.numeric(df$`rating B`)
df$`Student Overall Rating` <- as.numeric(df$`Student Overall Rating`)

str(df)  # shows relative structure I am dealing with
'data.frame':   3 obs. of  6 variables:
 $ rating A                : num  4 4 3
 $ rating B                : num  5 4 4
 $ Student Overall Rating  : num  4.5 4 3.5
 $ feedback 1              : chr  "blah" "blah" "blah"
 $ feedback 2              : chr  "blah" "blah" "blah"
 $ Student Overall Feedback: chr  "blahblah" "blahblah" "blahblah"

我进行了广泛的搜索,并try 了以下几种方法:

df <- df %>% select(!intersect(is.numeric(df), df %like% "Overall"))

这给了我:

Error in `select()`:
! Can't subset columns with `intersect(is.numeric(df), df %like% "Overall")`.
✖ `intersect(is.numeric(df), df %like% "Overall")` must be numeric or character, not `FALSE`.

我也试过...

df <- df %>% select(!where(is.numeric | contains("Overall")))

这将导致:

Error in `select()`:
! Problem while evaluating `where(is.numeric | contains("Overall"))`.
Caused by error in `is.numeric | contains("Overall")`:
! operations are possible only for numeric, logical or complex types

我希望得到的结果是,对于具有数字"学生总体评分"字段的文件:

'data.frame':   3 obs. of  5 variables:
 $ rating A                : num  4 4 3
 $ rating B                : num  5 4 4
 $ feedback 1              : chr  "blah" "blah" "blah"
 $ feedback 2              : chr  "blah" "blah" "blah"
 $ Student Overall Feedback: chr  "blahblah" "blahblah" "blahblah"

我知道我可以分别做select(where())个条件,但有没有办法让它同时满足两个条件呢?有没有其他方法可以做到这一点?我真的在努力避免手动操作每个文件.

推荐答案

你就差一步了!

library(dplyr)
df |> select(!(where(is.numeric) & contains("Overall")))
#   rating A rating B feedback 1 feedback 2 Student Overall Feedback
# 1        4        5       blah       blah                 blahblah
# 2        4        4       blah       blah                 blahblah
# 3        3        4       blah       blah                 blahblah

以下是一些解释:

df <- df %>% select(!intersect(is.numeric(df), df %like% "Overall"))

上面的失败是因为您正在应用is.numericdf,即数据帧.数据帧是list,不是数字,is.numeric(df)FALSE.%like%也一样.您需要处理列名,而不是数据框本身.

df <- df %>% select(!where(is.numeric | contains("Overall")))

这个很接近,它在我的工作解决方案中使用了!where个Like.这里的问题是where()想要在它里面有一个函数.is.numeric是一个函数,但is.numeric | contains("Overall")不是一个函数.把contains()insidewhere()放在一起也不太管用.where()将对您在每一列上给出的函数求值,一次一个.contains()预计会一次查看一组列的名称.

R相关问答推荐

如何在x轴下方画一条带有箭头的线?

按块将载体转换为矩阵-reshape

卸载安装了BRM的模型发出的警告

根据固定值范围在tible中添加新行

编辑文件后编辑RhandsonTable

在GGPLATE中将突出的点放在前面

如何在一次运行中使用count进行多列计数

R中插入符号训练函数的中心因子和尺度因子预测

如何在R库GoogleDrive中完全删除预先授权的Google帐户?

当我添加美学时,geom_point未对齐

R:用GGPLATE,如何在两个独立的变量中制作不同形状的散点图?

`夹心::vcovCL`不等于`AER::tobit`标准错误

在多页PDF中以特定布局排列的绘图列表不起作用

有没有办法一次粘贴所有列

我是否可以使用多个变异项来构建顺序列(标记多个问题)

无法将条件case_when()应用于使用!!创建的新变量Mutations

判断函数未加载R中的库

基于R中的辅助向量中的值有条件地连接向量中的字符串

在生成打印的自定义函数中,可以通过变量将线型或 colored颜色 设置为NULL吗?

位置_道奇在geom_point图中不躲避