Mutate function

Error message when using the mutate function, can anyone help?

no applicable method for 'mutate_' applied to an object of class "c('mts', 'ts')"

You are trying to use mutate on an object that is not a data frame, tibble or some other class the mutate can work on. The object seems to be some kind of time series object. Can you show the code that makes the object you are trying to mutate and explain what you want to do to it?

Okay thank you. I will put my code below - I was using the function mutate to compute a score for specific questions of my DASS-21 questionnaire. Ideally, I am looking to calculate individual participants scores for 3 categories of the DASS-21 (depression, anxiety and stress). The numbers in the final life of code are the questions which represent the depression scale

dass21<- data %>% select(1:29)

#Assigning dass to correct columns, column 1 is participant ID
dass21 <- data[,c(1, 2:29)]

#Excluding the participants with no answers for the dass
exclude <- c(12,17,18,19,20,21,25,34,35,36,40,60,65,67,71,72,77,78,82,86,102,103,112,113,115,117,119,120,121,123,128,129,130,131)

#This includes all other participants in the dass other than those removed
dass21 <- dass21 %>% filter(!(1 %in% exclude))

#Calculating scores for depression questions (Numbers different based on R columns )
dass21 <- dass21 %>% mutate(depression = rowSums(dass21[8 + c(11,13,18,21,24,25,29)]))

I have some comments about your code.
Where does the original data object come from? What is it? Posting the output of str(data) or summary(data) might help.

This line
dass21 <- dass21 %>% filter(!(1 %in% exclude))
does not do any filtering. The value of !(1 %in% exclude) is TRUE, so every row of dass21 will be returned.

Subsetting dass21 with dass21[8 + c(11,13,18,21,24,25,29)] seems to call for columns that do not exist. dass21 has 29 columns after you did
dass21 <- data %>% select(1:29).
The calculation 8 + c(11,13,18,21,24,25,29) will return 19, 21, 26, 29, 32, 33, 37

The original data is based on 131 participants and includes their answers to some demographic based questions and 3 psychometric tests.

Ah of course, I didn't think about that - I got my code from this website that suggested the '8' allows the analysis to begin from column 9 (where my Dass questionnaire begins) as this was what I was trying to achieve.

I am guessing you want to do something like the following code. I assumed you have a column with an id number for the participant and it is these numbers that you use in the exclude vector. I chose the name ParticipantID for that column. You will need to change that to the correct name.

dass21<- data %>% select(1:29)
exclude <- c(12,17,18,19,20,21,25,34,35,36,40,60,65,67,71,72,77,
dass21 <- dass21 %>% filter(!(ParticipantID %in% exclude))
dass21 <- dass21 %>% mutate(depression = rowSums(dass21[c(11,13,18,21,24,25,29)]))

Yes that is right, thank you very much I will try this!

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.