R语言 是否删除值/字符不超过7个的行?[副本]

t0ybt7op  于 2023-05-20  发布在  其他
关注(0)|答案(1)|浏览(238)

此问题已在此处有答案

Remove all rows where length of string is more than n(4个答案)
13小时前关闭
我们在“标识符”列中有值,其值范围为7-11个字符/数字。是否有方法删除值超过9个字符的行?

Identifier    X1  X2  X3  X4
2021047.PR    0   0   1   9
2021047.YB    3   0   3   0
2021047.C     0   5   0   3
2020048       8   6   2   0
2021049.Arv   7   0   3   0
2021049.BR    0   3   0   8
2021049.C     3   0   0   1
2021050       4   0   7   0

预期结果:首选Dplyr/Tidy结果

Identifier    X1  X2  X3  X4
2021047.C     0   5   0   3
2020048       8   6   2   0
2021049.C     3   0   0   1
2021050       4   0   7   0
structure(list(Identifier = c("2021047.PR", "2021047.YB", "2021047.C", 
"2020048", "2021049.Arv", "2021049.BR", "2021049.C", "2021050"
), X1 = c(0L, 3L, 0L, 8L, 7L, 0L, 3L, 4L), X2 = c(0L, 0L, 5L, 
6L, 0L, 3L, 0L, 0L), X3 = c(1L, 3L, 0L, 2L, 3L, 0L, 0L, 7L), 
    X4 = c(9L, 0L, 3L, 0L, 0L, 8L, 1L, 0L)), class = "data.frame", row.names = c(NA, 
-8L))
nmpmafwu

nmpmafwu1#

使用nchar获取字符串的长度

> subset(df, nchar(Identifier) <= 9)
  Identifier X1 X2 X3 X4
3  2021047.C  0  5  0  3
4    2020048  8  6  2  0
7  2021049.C  3  0  0  1
8    2021050  4  0  7  0

相关问题