Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to assign a value to a data.frame filtered by dplyr?

Tags:

r

dplyr

I am trying to modify a data.frame filtered by dplyr but I don't quite seem to grasp what I need to do. In the following example, I am trying to filter the data frame z and then assign a new value to the third column -- I give two examples, one with "9" and one with "NA".

require(dplyr)
z <- data.frame(w = c("a", "a", "a", "b", "c"), x = 1:5, y = c("a", "b", "c", "d", "e"))
z %>% filter(w == "a" & x == 2) %>% select(y) 
z %>% filter(w == "a" & x == 2) %>% select(y) <- 9 # Should be similar to z[z$w == "a" & z$ x == 2, 3] <- 9
z %>% filter(w == "a" & x == 3) %>% select(y) <- NA # Should be similar to z[z$w == "a" & z$ x == 3, 3] <- NA

Yet, it doesn't work: I get the following error message:

"Error in z %>% filter(w == "a" & x == 3) %>% select(y) <- NA : impossible de trouver la fonction "%>%<-"

I know that I can use the old data.frame notation, but what would be the solution for dplyr?

Thanks!

like image 855
nullepart Avatar asked Apr 28 '16 20:04

nullepart


People also ask

What does %>% do in dplyr?

%>% is called the forward pipe operator in R. It provides a mechanism for chaining commands with a new forward-pipe operator, %>%. This operator will forward a value, or the result of an expression, into the next function call/expression. It is defined by the package magrittr (CRAN) and is heavily used by dplyr (CRAN).

How do you subset data in R dplyr?

In order to Filter or subset rows in R we will be using Dplyr package. Dplyr package in R is provided with filter() function which subsets the rows with multiple conditions on different criteria. We will be using mtcars data to depict the example of filtering or subsetting. Filter or subset the rows in R using dplyr.

Does dplyr work with data frame?

All of the dplyr functions take a data frame (or tibble) as the first argument. Rather than forcing the user to either save intermediate objects or nest functions, dplyr provides the %>% operator from magrittr. x %>% f(y) turns into f(x, y) so the result from one step is then “piped” into the next step.

Can you use filter on a data frame in R?

How to apply a filter on dataframe in R ? A filter () function is used to filter out specified elements from a dataframe that return TRUE value for the given condition (s). filter () helps to reduce a huge dataset into small chunks of datasets.


1 Answers

Filtering will subset the data frame. If you want to keep the whole data frame, but modify part of it, you can, for example use mutate with ifelse. I've added stringsAsFactors=FALSE to your sample data so that y will be a character column.

z <- data.frame(w = c("a", "a", "a", "b", "c"), x = 1:5, y = c("a", "b", "c", "d", "e"), 
                stringsAsFactors=FALSE)

z %>% mutate(y = ifelse(w=="a" & x==2, 9, y))
  w x y
1 a 1 a
2 a 2 9
3 a 3 c
4 b 4 d
5 c 5 e

Or with replace:

z %>% mutate(y = replace(y, w=="a" & x==2, 9),
             y = replace(y, w=="a" & x==3, NA)) 
  w x    y
1 a 1    a
2 a 2    9
3 a 3 <NA>
4 b 4    d
5 c 5    e
like image 107
eipi10 Avatar answered Oct 27 '22 06:10

eipi10