Using mutate function to recode() a column with new variables, getting 'error in parse', unexpected '='

I am working on my capstone project for Google Data Analytics Certificate, and trying to create a portfolio with R. I have come into a problem when trying to make a code chunk containing instructions to modify a column's variables. The code is using mutate() and recode(). When I work on this code chunk in Rstudio, it runs fine. But in kaggle, I am getting a 'parse error' and it tells me there is an 'unexpected =' in the code chunk.

q4_2016 <- q4_2016 %>%
    mutate(q4_2016$member_casual = recode(member_casual
    ,"Subscriber" = "member"
    ,"Customer" = "casual"))
Error in parse(text = x, srcfile = src): <text>:4:34: unexpected '='
3: q4_2016 <- q4_2016 %>%
4:     mutate(q4_2016$member_casual =
                                                                                ^

That is what it looks like. I tried changing the '=' after member_casual to <-, and it appeared to work at first, but I realized it created a null column that ruined the rest of the dataset.

Here is what that looks like:

Rows: 683,818
Columns: 20
$ ride_id            <chr> "12979228", "12979227", "12979226", "12979225", "12…
$ started_at         <dttm> 2016-12-31 23:57:52, 2016-12-31 23:53:18, 2016-12-…
$ ended_at           <dttm> 2017-01-01 00:06:44, 2017-01-01 00:08:13, 2017-01-…
$ rideable_type      <chr> "5076", "5114", "1026", "504", "4451", "5643", "48"…
$ tripduration       <int> 532, 895, 931, 970, 980, 179, 1863, 1867, 1656, 108…
$ start_station_id   <int> 502, 195, 195, 199, 199, 47, 177, 177, 195, 264, 15…
$ start_station_name <chr> "California Ave & Altgeld St", "Columbus Dr & Rando…
$ end_station_id     <int> 258, 25, 25, 35, 35, 125, 140, 140, 195, 52, 42, 77…
$ end_station_name   <chr> "Logan Blvd & Elston Ave", "Michigan Ave & Pearson …
$ member_casual      <chr> "Customer", "Customer", "Customer", "Subscriber", "…
$ `... <- NULL`      <chr> "casual", "casual", "casual", "member", "member", "…
$ start_date         <chr> "2016-12-31", "2016-12-31", "2016-12-31", "2016-12-…
$ start_time         <chr> "23:57:52", "23:53:18", "23:53:07", "23:51:31", "23…
$ end_date           <chr> "2017-01-01", "2017-01-01", "2017-01-01", "2017-01-…
$ end_time           <chr> "00:06:44", "00:08:13", "00:08:38", "00:07:41", "00…
$ date               <date> 2016-12-31, 2016-12-31, 2016-12-31, 2016-12-31, 20…
$ month              <chr> "12", "12", "12", "12", "12", "12", "12", "12", "12…
$ day                <chr> "31", "31", "31", "31", "31", "31", "31", "31", "31…
$ year               <chr> "2016", "2016", "2016", "2016", "2016", "2016", "20…
$ day_of_week        <chr> "Saturday", "Saturday", "Saturday", "Saturday", "Sa…

The '… <- NULL' column contains the correct variables that I want to be under the 'member_casual' column.
If this isn't the appropriate place to ask, I apologize. If anyone can help me it would be much appreciated. Thanks

In mutate, you don't need to specify the dataframe name again:

q4_2016 %>%
    mutate(member_casual = recode(member_casual ...

Thanks. I just tried out the new code chunk and it returned another error. It says it could not find the '%>%' function to be used.

I worked on it a bit and found out that if I input library(dplyr) before the code chunk, it runs fine the way you showed me to write it. Thanks!

Yes, %>% is defined in a library, specifically in {magrittr}. Loading {dplyr} automatically imports %>%.

If you wanted to get rid of that dependency, you could try replacing %>% with |>, the native R pipe, which is mostly equivalent. You probably don't need to worry about that for now, since you need to load {dplyr} anyway to use mutate().

1 Like

Thanks for the advice! I didn't even know about |>. You have been greatly helpful.

This topic was automatically closed 7 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.