r - 按名称条件删除列
问题描述
我是 R 新手,我需要解决这个问题,我有一个数据框,其列名具有这种模式:
#Example
¦ 1.1 ¦ 1.2 ¦ 1.3 ¦ 2.1 ¦ 2.2 ¦ 2.3 ¦ 3.1 ¦ 3.2 ¦ 3.3 ¦
如何删除名称具有此条件的数据框中的所有列:
#Suppose x.y colname
(x.y)
if x>y => delete column
后:
¦ 1.1 ¦ 1.2 ¦ 1.3 ¦ 2.2 ¦ 2.3 ¦ 3.3 ¦
这是 dput(head(x)) 的输出,其中 x 是我的 df: “Código UNU”只是一个 ID
我尝试了 grep 但我做不到。所有的帮助都将受到欢迎和感激!
解决方案
您可以使用 baseR
根据此条件对列进行索引:
names = as.character(c(1.1, 1.2, 1.3, 2.1, 2.2, 2.3, 3.1, 3.2, 3.3))
df = setNames(rnorm(n = length(names)), names)
df
# 1.1 1.2 1.3 2.1 2.2 2.3 3.1 3.2 3.3
# -0.4685751 -0.1085529 -0.5613519 -1.0906374 1.0530686 -0.8101930 -0.6015732 1.3895373 -0.6977108
wrangle <- function(x) {
list <- strsplit(x, split = "\\.")
left <- list[[1]][1]
right <- list[[1]][2]
return(left <= right) #will be TRUE if desired criteria is met
}
df[unlist(lapply(names, wrangle))] #index using the T/F vector
# 1.1 1.2 1.3 2.2 2.3 3.3
# -0.9873006 0.6089725 0.2823161 0.3397318 -0.3136084 0.2270087
推荐阅读
- python - 在 Dockerfile 中安装 pandas
- python - python生成固定大小(16字节)的真实随机字符串
- laravel - 如何从 laravel 中的密码重置链接中删除 localhost
- python - 我想通过表单数据发送邮件。我希望接收者被固定,而发送者只改变。我怎样才能做到这一点?
- python - 解压多个值并存储到数据库中失败
- python - VSCode 调试器自动附加到子进程
- tensorflow - TensorBoard 检查时,为什么“tf.nn.bidirectional_dynamic_rnn”在 TPU 上有“不兼容的操作”?
- c# - 如何在 C# 中创建此等效列表?
- java - Maven 从多个位置扫描类型的警告
- networking - 为什么 ip 192.168.1.1 是路由器的默认 IP?