Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 如何子集包含空白项的表_R - Fatal编程技术网

R 如何子集包含空白项的表

R 如何子集包含空白项的表,r,R,我有一个包含5列和>100k行的表 col1 col2 col3 col4 col5 foo bar foo1 bar1 this1 that1 other1 foo2 bar2 that2 other2 我想在这张表中读一下,以便空格是 读入为NA,或 这些行将被忽略 我尝试了a=read.table(“a.txt”,header=F,sep='\t',na.strings=c(“na”,“NULL”),但发

我有一个包含5列和>100k行的表

col1    col2    col3    col4    col5
foo     bar
foo1    bar1    this1  that1    other1
foo2    bar2           that2    other2
我想在这张表中读一下,以便空格是

  • 读入为NA,或
  • 这些行将被忽略
  • 我尝试了
    a=read.table(“a.txt”,header=F,sep='\t',na.strings=c(“na”,“NULL”)
    ,但发现
    na
    只在整数列中出现,而不是字符串。我一直在尝试将结果表子集,以删除带有空格的列,但尚未成功

    我试过:

    subset(a,a$V4!= ' ')
    subset(a,a$V4!= '\t')
    subset(a,a$V4!= '\w')
    subset(a,a$V4!= '\s')
    subset(a,a$V4==NULL)
    subset(a,a$V4==is.na) 
    
    都没有用


    如果您能根据@adibender所说的内容提供任何建议,我将不胜感激。您可能在这些空白字段中有未知数量的空白字符。包含
    c(“”,”)
    可能会解决某些字段的问题,但如果空格字符数不同,仍然会有一些空格

    R在比较中不使用正则表达式,就像您在上面的
    子集(a,a$V4!='\s')
    中所做的那样。您可以使用
    grepl
    进行正则表达式比较并返回逻辑答案,例如
    a[!grepl('[:space:]',a$V4),]
    (返回
    a
    中未找到空格字符的行
    a


    如果要删除空白并替换为NA(假设没有有效的空白),我可能会从
    stringr
    中使用
    gsub
    stru trim
    ,然后使用
    ifelse(a$V4='',NA,a$V4)
    将空字符串更改为
    NA

    是否可以显示示例数据?请尝试
    NA.strings=c(“,”)