'No applicable method for mutate?

Currently trying to use sample R code and implement it into my own Sample code goes like this:

syn_data <- syn_data %>% 
  dplyr::mutate(gender = factor(gender,
                                labels = c("female", "male")))

My code goes:

data <- data %>%
    dplyr::mutate(condition = factor(condition,
                                labels = c("Fixed Ratio 6", "Variable Ratio 6", "Fixed Interval 8", "Variable Interval 8")))

Getting this error:

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

Edit:

categorical. Reinforcement schedule the rat has been assigned to: 0 = 'Fixed Ratio 6'; 1 = 'Variable Ratio 6'; 2 = 'Fixed Interval 8'; 3 = 'Variable Interval 8'.

Data (sample right, mine left)

data

r


Solution 1:[1]

The cause of your problem is that data is not a data.frame, which is the required class for the first argument of mutate. If you change it to a data.frame, your code works.

For example:

tap_data <- data.frame(rat_id = 1:4, condition = c(0,1,2,3))
tap_data <- tap_data %>% mutate(condition = factor(condition,
                                labels = c("Fixed Ratio 6", "Variable Ratio 6", 
                                           "Fixed Interval 8", "Variable Interval 8")))
tap_data
#     rat_id           condition
# 1      1       Fixed Ratio 6
# 2      2    Variable Ratio 6
# 3      3    Fixed Interval 8
# 4      4 Variable Interval 8

To check if an object is a data.frame, you can use is.data.frame(). You can check for some other classes with similar syntax, such as is.factor().

is.data.frame(tap_data)
#[1] TRUE

is.data.frame(tap_data$condition)
# [1] FALSE

is.factor(tap_data$condition)
#[1] TRUE

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Abdur Rohman