我有一个具有7个标头的test.txt文件。但并非每一行都有7个值。 我的txt文件仅显示1列,我使用read.delim分隔了这些列。但是,当我计算每列的唯一值时,我得到的输出与我期望的不同
df_test <- read.delim("Test.txt", sep = "")
#c("ID", "colour", "skin", "eyes", "country", "name", "size"))
testing <- unique(df_test$ID)
A tibble: 54 x 1
testing
<fct>
1 EGA.0
2 blue
3 green
4 ""
5 V-alpha
6 EGA.1
7 EGA.2
8 EGA.3
9 EGA.4
10 EGA.5
我只希望ID列中以EGA开头的值。我该如何解决?如何处理没有7个值的列?我不希望在“ ID”中使用“颜色”值...
testing <- unique(df_test$ID)
A tibble: 54 x 1
testing
<fct>
1 EGA.0
6 EGA.1
7 EGA.2
8 EGA.3
9 EGA.4
10 EGA.5
答案 0 :(得分:2)
我们可以使用str_detect
library(tidyverse)
testing %>%
filter(str_detect(testing, "^EGA\\.\\d+"))
# A tibble: 6 x 1
# testing
# <fct>
#1 EGA.0
#2 EGA.1
#3 EGA.2
#4 EGA.3
#5 EGA.4
#6 EGA.5
testing <- structure(list(testing = structure(c(3L, 2L, 9L, 1L, 10L, 4L,
5L, 6L, 7L, 8L), .Label = c("", "blue", "EGA.0", "EGA.1", "EGA.2",
"EGA.3", "EGA.4", "EGA.5", "green", "V-alpha"), class = "factor")),
row.names = c(NA,
-10L), class = c("tbl_df", "tbl", "data.frame"))