"Error in UseMethod("mutate") : no applicable method for 'mutate' applied to an object of class "function" when trying to seperate columns

Viewed 15427

So I have this dataset

# A tibble: 268 x 1
   `Which of these social media platforms do you have an account in right now?`
   <chr>                                                                       
 1 Facebook, Instagram, Twitter, Snapchat, Reddit, Signal                      
 2 Reddit                                                                      
 3 Facebook, Instagram, Twitter, Linkedin, Snapchat, Reddit, Quora             
 4 Facebook, Instagram, Twitter, Snapchat                                      
 5 Facebook, Instagram, TikTok, Snapchat                                       
 6 Facebook, Instagram, Twitter, Linkedin, Snapchat                            
 7 Facebook, Instagram, TikTok, Linkedin, Snapchat, Reddit                     
 8 Facebook, Instagram, Snapchat                                               
 9 Linkedin, Reddit                                                            
10 Facebook, Instagram, Twitter, TikTok                                        
# ... with 258 more rows

and I want to break this into multiple columns with yes and no on each variable like this

# A tibble: 268 x 8
      Id Facebook Instagram Reddit Signal Snapchat TikTok Twitter
   <int> <chr>    <chr>     <chr>  <chr>  <chr>    <chr>  <chr>  
 1     1 No       No        No     No     No       No     Yes    
 2     2 Yes      Yes       No     No     Yes      No     Yes    
 3     3 No       Yes       No     Yes    No       Yes    No     
 4     4 No       Yes       No     No     Yes      No     No     
 5     5 No       Yes       No     Yes    Yes      Yes    Yes    
 6     6 No       Yes       No     No     No       No     No     
 7     7 No       No        Yes    Yes    No       Yes    Yes    
 8     8 No       No        Yes    No     No       No     Yes    
 9     9 No       No        Yes    No     Yes      Yes    No     
10    10 No       Yes       Yes    Yes    Yes      No     Yes

So I wrote this code to do so

library(tidyverse)
library(tidytext)

Survey %>%
  mutate(Id = row_number(), HasAccount = "Yes") %>%
  unnest_tokens(Network, `Which of these social media platforms do you have an account in right now?`, to_lower = F) %>%
  spread(Network, HasAccount, fill = "No")

but I get this error

Erreur : Must extract column with a single valid subscript.
x Subscript `var` has size 268 but must be size 1.
> dput(head(Survey[1:5]))
structure(list(Horodateur = structure(c(1619171956.596, 1619172695.039, 
1619173104.83, 1619174548.534, 1619174557.538, 1619174735.457
), tzone = "UTC", class = c("POSIXct", "POSIXt")), `To_which_gender_you_identify_the_most?` = c("Male", 
"Female", "Male", "Female", "Female", "Female"), What_is_your_age_group = c("[18-24[", 
"[10,18[", "[18-24[", "[18-24[", "[18-24[", "[25,34["), How_much_time_do_you_spend_on_social_media = c("1-5 hours", 
"1-5 hours", ">10 hours", "5-10 hours", "5-10 hours", "1-5 hours"
), `Which_of_these_social_media_platforms_do_you_have_an_account_in_right_now?` = c("Facebook, Instagram, Twitter, Snapchat, Reddit, Signal", 
"Reddit", "Facebook, Instagram, Twitter, Linkedin, Snapchat, Reddit, Quora", 
"Facebook, Instagram, Twitter, Snapchat", "Facebook, Instagram, TikTok, Snapchat", 
"Facebook, Instagram, Twitter, Linkedin, Snapchat")), row.names = c(NA, 
-6L), class = c("tbl_df", "tbl", "data.frame"))

EDIT: Edited the question based on @CSJCampbell's answer. EDIT: Added a snippet of the dataset I'm working with.

2 Answers

The first argument to mutate must be a data.frame. You did not name your data.frame df, so the function df is passed to mutate.

args(df)
# function (x, df1, df2, ncp, log = FALSE) 
# NULL

EDIT: After your update you added dput output of your data. Running your code gives me the error:

Survey %>%
    mutate(Id = row_number(), HasAccount = "Yes") %>%
    unnest_tokens(Network, `Which of these social media platforms do you have an account in right now?`, to_lower = F)
# Error in check_input(x) : 
# Input must be a character vector of any length or a list of character
# vectors, each of which has a length of 1.

Your dput has the column named with underscores:

colnames(Survey)[5]
# "Which_of_these_social_media_platforms_do_you_have_an_account_in_right_now?"

Renaming the column:

Survey %>%
    transmute(Id = row_number(), HasAccount = "Yes", 
        Platforms = `Which_of_these_social_media_platforms_do_you_have_an_account_in_right_now?`) %>% 
    unnest_tokens(Network, Platforms) %>% 
    spread(Network, HasAccount, fill = "No")
# # A tibble: 6 x 10
#      Id facebook instagram linkedin quora reddit
#   <int> <chr>    <chr>     <chr>    <chr> <chr> 
# 1     1 Yes      Yes       No       No    Yes   
# 2     2 No       No        No       No    Yes   
# 3     3 Yes      Yes       Yes      Yes   Yes   
# 4     4 Yes      Yes       No       No    No    
# 5     5 Yes      Yes       No       No    No    
# 6     6 Yes      Yes       Yes      No    No    
# # … with 4 more variables: signal <chr>,
# #   snapchat <chr>, tiktok <chr>, twitter <chr>

(Not an answer to this exact question, but similar enough to the post title that I think it's relevant...)

I ran into a very similar error for a slightly different (but related) reason, namely, I mixed up ggplot and pipe (%>%) syntax. I wrote:

df %>%                 # <- pipe
   summarize(...) +    # <- NOT a pipe!
   mutate(...)

resulting in the error:

Error in UseMethod("mutate") : no applicable method for 'mutate' applied to an object of class "name"

This is, as in the accepted answer, due to mutate() not getting a data frame as its first argument. I'm adding this as an answer rather than a comment to highlight this kind of mistake which can be easy to make when working within the tidyverse, and especially when switching between manipulating data and updating plots, but which might not be obvious at first glance.

Related