Problems with filter()

I have used filter() several times, but something happened to the packages after updating them with: update.packages(ask = FALSE, checkBuilt = TRUE)

Now I need to use the filter function, but for some reason it doesn't run as it should; I have used the function several times and I don't understand why it happens.

I have this table:

DS90_2

    Parametro           Tabla   Valor
1	Aluminio total (Al)	Tabla 1	5.00
2	Aluminio total (Al)	Tabla 2	10.00
3	Aluminio total (Al)	Tabla 3	1.00
4	Aluminio total (Al)	Tabla 4	1.00
5	Aluminio total (Al)	Tabla 5	10.00
6	Arsénico total (As)	Tabla 1	0.50
7	Arsénico total (As)	Tabla 2	1.00
8	Arsénico total (As)	Tabla 3	0.10
9	Arsénico total (As)	Tabla 4	0.20
10	Arsénico total (As)	Tabla 5	0.50

I need create another table only with data from "Aluminio total (Al)". So, I used filter():

Al_DS90<-filter(DS90_2,Parametro=="Aluminio total (Al) ")

But the result is a table without data: 0 obs. with 3 variables.

Thank you very much for your help.

I notice there is a space between (Al) and the closing " in your filter command. Could that be the problem?

In adition to @FJCC comment, I tend to filter in a different way, I would do:

Al_DS90<-DS90_2[DS90_2$Parametro=="Aluminio total (Al)", ]

cheers
F.

better you soult do some simple checks, like (table(unique).....)

cheers

The following code works for me.

DF <- structure(list(Parametro = c("Aluminio total (Al)", "Aluminio total (Al)", 
                                   "Aluminio total (Al)", "Aluminio total (Al)", 
                                   "Aluminio total (Al)", "Arsénico total (As)", 
                                   "Arsénico total (As)", "Arsénico total (As)", 
                                   "Arsénico total (As)", "Arsénico total (As)"), 
                     Tabla = c("Tabla 1","Tabla 2", "Tabla 3", "Tabla 4", "Tabla 5", 
                               "Tabla 1", "Tabla 2","Tabla 3", "Tabla 4", "Tabla 5"), 
                     Valor = c(5, 10, 1, 1, 10,0.5, 1, 0.1, 0.2, 0.5)), 
                row.names = c(NA, -10L), class = c("tbl_df","tbl", "data.frame"))
library(dplyr, warn.conflicts = FALSE)
AL <- filter(DF, Parametro == "Aluminio total (Al)")
AL

What happens if you run exactly that code?

1 Like

It works well, it manages to filter out what I need.

But I still don't understand why I can't do it the direct way.

Thank you very much!

a locale or encoding issue perhaps

I don't know.

This is de original tibble:

> DS90
# A tibble: 10 x 6
   Parametro            `Tabla 1` `Tabla 2` `Tabla 3` `Tabla 4` `Tabla 5`
   <chr>                    <dbl>     <dbl>     <dbl>     <dbl>     <dbl>
 1 Aluminio total (Al)       5         10        1         1        10   
 2 Arsénico total (As)       0.5        1        0.1       0.2       0.5 
 3 Cadmio total (Cd)         0.01       0.3      0.02      0.02      0.5 
 4 Cinc total (Zn)           3         20        5         5         5   
 5 Cobre total (Cu)          1          3        0.1       1         3   
 6 Cromo total (Cr)         NA         NA        2.5       2.5      10   
 7 Manganeso total (Mn)      0.3        3        0.5       2         4   
 8 NĂ­quel total (Ni)         0.2        3        0.5       2         4   
 9 Plomo total (Pb)          0.05       0.5      0.2       0.2       1   
10 Selenio total (Se)        0.01       0.1      0.01      0.01      0.03
Warning message:
`...` is not empty.

We detected these problematic arguments:
* `needs_dots`

These dots only exist to allow future extensions and should be empty.
Did you misspecify an argument? 

This is the case with the tibble I created (DS90):

> DS90%>%filter(Parametro=="Aluminio total (Al)")
# A tibble: 0 x 6
# ... with 6 variables: Parametro <chr>, `Tabla 1` <dbl>, `Tabla 2` <dbl>, `Tabla
#   3` <dbl>, `Tabla 4` <dbl>, `Tabla 5` <dbl>
Warning message:
`...` is not empty.

We detected these problematic arguments:
* `needs_dots`

These dots only exist to allow future extensions and should be empty.
Did you misspecify an argument?

If I saw this sort of thing happening from merely printing to console my data.frame, I would assume my R session got corrupted in someway and use Ctrl+Shift+F10 to restart the session, and begin again.

2 Likes

This thread continues on this new topic:

1 Like

Thank you. I'll try that.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.

Thank you very much, I used your code but it still gives me a table with no results.

When I reloaded some packages, the tidyverse package generated conflicts about filter():

-- Conflicts ------------------------------------------ tidyverse_conflicts() --
x dplyr::filter() masks stats::filter()
x dplyr::lag()    masks stats::lag()

Regards,

Thanks, I corrected the space (on the console it was without the space) and it's still the same, it doesn't give me any results.

When I reloaded some packages, the tidyverse package generated conflicts about filter():

-- Conflicts ------------------------------------------ tidyverse_conflicts() --
x dplyr::filter() masks stats::filter()
x dplyr::lag()    masks stats::lag()

Regards,

Sorry mate, I have no idea in regards tidyverse packages etc.... If you use them, then you better know the conflicts. I have never use tidyverse....
cheers
Fer

I made a csv version of the data you posted and the follwoing code works

DF <- read.csv("~/R/Play/Dummy.csv")
library(dplyr, warn.conflicts = FALSE)
AL <- filter(DF, Parametro == "Aluminio total (Al)")

Perhaps the data I made is not exactly like your data. Please post the output of

dput(head(DS90_2, 10))

so I can work with a copy of your data.

The warnings about conflicts are completely normal.

I used your code but it didn't work. I used filter() on other tables with the same code I commented at the beginning, (I just replaced the data source table) and it worked fine. So I think the problem is with my table.

The output is:

structure(list(Parametro = c("Aluminio total (Al)", "Aluminio total (Al)", 
"Aluminio total (Al)", "Aluminio total (Al)", "Aluminio total (Al)", 
"Arsénico total (As)", "Arsénico total (As)", "Arsénico total (As)", 
"Arsénico total (As)", "Arsénico total (As)"), Tabla = c("Tabla 1", 
"Tabla 2", "Tabla 3", "Tabla 4", "Tabla 5", "Tabla 1", "Tabla 2", 
"Tabla 3", "Tabla 4", "Tabla 5"), Valor = c(5, 10, 1, 1, 10, 
0.5, 1, 0.1, 0.2, 0.5)), row.names = c(NA, -10L), class = c("tbl_df", 
"tbl", "data.frame"))

All the data is

1	Aluminio total (Al)	Tabla 1	5.00
2	Aluminio total (Al)	Tabla 2	10.00
3	Aluminio total (Al)	Tabla 3	1.00
4	Aluminio total (Al)	Tabla 4	1.00
5	Aluminio total (Al)	Tabla 5	10.00
6	Arsénico total (As)	Tabla 1	0.50
7	Arsénico total (As)	Tabla 2	1.00
8	Arsénico total (As)	Tabla 3	0.10
9	Arsénico total (As)	Tabla 4	0.20
10	Arsénico total (As)	Tabla 5	0.50
11	Cadmio total (Cd)	Tabla 1	0.01
12	Cadmio total (Cd)	Tabla 2	0.30
13	Cadmio total (Cd)	Tabla 3	0.02
14	Cadmio total (Cd)	Tabla 4	0.02
15	Cadmio total (Cd)	Tabla 5	0.50
16	Cinc total (Zn)	Tabla 1	3.00
17	Cinc total (Zn)	Tabla 2	20.00
18	Cinc total (Zn)	Tabla 3	5.00
19	Cinc total (Zn)	Tabla 4	5.00
20	Cinc total (Zn)	Tabla 5	5.00
21	Cobre total (Cu)	Tabla 1	1.00
22	Cobre total (Cu)	Tabla 2	3.00
23	Cobre total (Cu)	Tabla 3	0.10
24	Cobre total (Cu)	Tabla 4	1.00
25	Cobre total (Cu)	Tabla 5	3.00
26	Cromo total (Cr)	Tabla 1	NA
27	Cromo total (Cr)	Tabla 2	NA
28	Cromo total (Cr)	Tabla 3	2.50
29	Cromo total (Cr)	Tabla 4	2.50
30	Cromo total (Cr)	Tabla 5	10.00
31	Manganeso total (Mn)	Tabla 1	0.30
32	Manganeso total (Mn)	Tabla 2	3.00
33	Manganeso total (Mn)	Tabla 3	0.50
34	Manganeso total (Mn)	Tabla 4	2.00
35	Manganeso total (Mn)	Tabla 5	4.00
36	NĂ­quel total (Ni)	Tabla 1	0.20
37	NĂ­quel total (Ni)	Tabla 2	3.00
38	NĂ­quel total (Ni)	Tabla 3	0.50
39	NĂ­quel total (Ni)	Tabla 4	2.00
40	NĂ­quel total (Ni)	Tabla 5	4.00
41	Plomo total (Pb)	Tabla 1	0.05
42	Plomo total (Pb)	Tabla 2	0.50
43	Plomo total (Pb)	Tabla 3	0.20
44	Plomo total (Pb)	Tabla 4	0.20
45	Plomo total (Pb)	Tabla 5	1.00
46	Selenio total (Se)	Tabla 1	0.01
47	Selenio total (Se)	Tabla 2	0.10
48	Selenio total (Se)	Tabla 3	0.01
49	Selenio total (Se)	Tabla 4	0.01
50	Selenio total (Se)	Tabla 5	0.03

Thank you very much for your help.

1 Like