filter in dplyr returning error

2019-09-06 07:17发布

问题:

Anyone have an idea why this is happening? I've got a dataframe and I'm trying to filter it with dplyr.

filter(dft, form=="9S")
Error in structure(as.character(x), names = names(x), dim = dim(x), dimnames = dimnames(x)) : 
  'names' attribute [1020] must be the same length as the vector [113]

Here is the class of the thing I'm trying to factor:

class(dft$form)
[1] "factor"

So...Not sure what the problem is..dplyr should be expecting factors, and I've seen plenty of examples online of people using dplyr's filter with strings to do this very thing.

Here's the head of the dft

   lab type form class     ami dateStarted GE-000 ANSI-000 ANSI-001 ANSI-002 ANSI-003 ANSI-004 ANSI-005 ANSI-006 ANSI-007 ANSI-008 ANSI-009
V3 2078 KV2c  16S   200 Flexnet   9/21/2010   <NA>     <NA>     <NA>     <NA>     <NA>     <NA>     <NA>     <NA>     <NA>     <NA>     <NA>
V4 2078 KV2c  16S   200 Flexnet   9/21/2010   <NA>     <NA>     <NA>     <NA>     <NA>     <NA>     <NA>     <NA>     <NA>     <NA>     <NA>
V5 2078 KV2c  12S   200 Flexnet   9/21/2010   <NA>     <NA>     <NA>     <NA>     <NA>     <NA>     <NA>     <NA>     <NA>     <NA>     <NA>
V6 2078 KV2c   9S    20 Flexnet   9/21/2010   <NA>     <NA>     <NA>     <NA>     <NA>     <NA>     <NA>     <NA>     <NA>     <NA>     <NA>
V7 2078 KV2c  36S    20 Flexnet   9/21/2010   <NA>     <NA>     <NA>     <NA>     <NA>     <NA>     <NA>     <NA>     <NA>     <NA>     <NA>
V8 2078 KV2c  45S    20 Flexnet   9/21/2010   <NA>     <NA>     <NA>     <NA>     <NA>     <NA>     <NA>     <NA>     <NA>     <NA>     <NA>
   ANSI-010 ANSI-011 ANSI-013 ANSI-014 ANSI-015 ANSI-016 ANSI-017 ANSI-018 ANSI-019 ANSI-020 ANSI-021 ANSI-022 ANSI-023 ANSI-024 ANSI-025
V3     <NA>     <NA>     <NA>     <NA>     <NA>     <NA>     <NA>     <NA>     <NA>     <NA>     <NA>     <NA>     <NA>     <NA>     <NA>
V4     <NA>     <NA>     <NA>     <NA>     <NA>     <NA>     <NA>     <NA>     <NA>     <NA>     <NA>     <NA>     <NA>     <NA>     <NA>
V5     <NA>     <NA>     <NA>     <NA>     <NA>     <NA>     <NA>     <NA>     <NA>     <NA>     <NA>     <NA>     <NA>     <NA>     <NA>
V6     <NA>     <NA>     <NA>     <NA>     <NA>     <NA>     <NA>     <NA>     <NA>     <NA>     <NA>     <NA>     <NA>     <NA>     <NA>
V7     <NA>     <NA>     <NA>     <NA>     <NA>     <NA>     <NA>     <NA>     <NA>     <NA>     <NA>     <NA>     <NA>     <NA>     <NA>
V8     <NA>     <NA>     <NA>     <NA>     <NA>     <NA>     <NA>     <NA>     <NA>     <NA>     <NA>     <NA>     <NA>     <NA>     <NA>
   ANSI-025a ANSI-026 ANSI-027 ANSI-028 ANSI-030 ANSI-031 ANSI-032 ANSI-033 ANSI-003a ANSI-034 ANSI-035 ANSI-038 GE-056 GE-070 GE-071 GE-076
V3      <NA>     <NA>     <NA>     <NA>     <NA>     <NA>     <NA>     <NA>      <NA>     <NA>     <NA>     <NA>      1   <NA>   <NA>      0
V4      <NA>        1     <NA>     <NA>     <NA>     <NA>     <NA>     <NA>      <NA>     <NA>     <NA>     <NA>      1   <NA>   <NA>      0
V5      <NA>        1     <NA>     <NA>     <NA>     <NA>     <NA>     <NA>      <NA>     <NA>     <NA>     <NA>      1   <NA>   <NA>      0
V6      <NA>        1     <NA>     <NA>     <NA>     <NA>     <NA>     <NA>      <NA>     <NA>     <NA>     <NA>      1   <NA>   <NA>      0
V7      <NA>     <NA>     <NA>     <NA>     <NA>     <NA>     <NA>     <NA>      <NA>     <NA>     <NA>     <NA>      1   <NA>   <NA>      0
V8      <NA>     <NA>     <NA>     <NA>     <NA>     <NA>     <NA>     <NA>      <NA>     <NA>     <NA>     <NA>      1   <NA>   <NA>      0
   GE-099 GE-102 GE-103 GE-105 GE-108 GE-119 GE-164
V3      1      1   <NA>   <NA>   <NA>   <NA>   <NA>
V4      1      1   <NA>   <NA>   <NA>   <NA>   <NA>
V5      1      1   <NA>   <NA>   <NA>   <NA>   <NA>
V6      1      1   <NA>   <NA>   <NA>   <NA>   <NA>
V7      1      1   <NA>   <NA>   <NA>   <NA>   <NA>
V8      1      1   <NA>   <NA>   <NA>   <NA>   <NA>

回答1:

Are you sure it's using dplyr's filter? Try dplyr::filter to force it to use the dplyr version.

Here I read in some of your data and try the same command and it works as expected:

dft <- read.table(text="   lab type form class     ami dateStarted GE-000 ANSI-000 ANSI-001 ANSI-002 ANSI-003 ANSI-004 ANSI-005 ANSI-006 ANSI-007 ANSI-008 ANSI-009
V3 2078 KV2c  16S   200 Flexnet   9/21/2010   <NA>     <NA>     <NA>     <NA>     <NA>     <NA>     <NA>     <NA>     <NA>     <NA>     <NA>
V4 2078 KV2c  16S   200 Flexnet   9/21/2010   <NA>     <NA>     <NA>     <NA>     <NA>     <NA>     <NA>     <NA>     <NA>     <NA>     <NA>
V5 2078 KV2c  12S   200 Flexnet   9/21/2010   <NA>     <NA>     <NA>     <NA>     <NA>     <NA>     <NA>     <NA>     <NA>     <NA>     <NA>
V6 2078 KV2c   9S    20 Flexnet   9/21/2010   <NA>     <NA>     <NA>     <NA>     <NA>     <NA>     <NA>     <NA>     <NA>     <NA>     <NA>
V7 2078 KV2c  36S    20 Flexnet   9/21/2010   <NA>     <NA>     <NA>     <NA>     <NA>     <NA>     <NA>     <NA>     <NA>     <NA>     <NA>
V8 2078 KV2c  45S    20 Flexnet   9/21/2010   <NA>     <NA>     <NA>     <NA>     <NA>  <NA>     <NA>     <NA>     <NA>     <NA>     <NA>", header=TRUE)

library(dplyr)
filter(dft, form=="9S")

## > filter(dft, form=="9S")
##    lab type form class     ami dateStarted GE.000 ANSI.000 ANSI.001 ANSI.002
## 1 2078 KV2c   9S    20 Flexnet   9/21/2010   <NA>     <NA>     <NA>     <NA>
##   ANSI.003 ANSI.004 ANSI.005 ANSI.006 ANSI.007 ANSI.008 ANSI.009
## 1     <NA>     <NA>     <NA>     <NA>     <NA>     <NA>     <NA>

If this doesn't work try restarting R in a clean session, perhaps you've overwritten something you ought not to have overwritten.