Mutate to ordinal Data

Hi I'm very new to R, so I have some basic questions.

I want to sort ordinal data (a factor) from 104 observations in 43 variables (first is the Name of the Observation and 42 variables ) coded as 1, 2, and 3. Naturally R thinks it is a numerical data. So first question: is there an easy way to change the data type for every variable after the first at once?

I found this code:

mutate_if(is.numeric, ~as.factor(as.character(.)))

or

mutate_at(vars(Clavicula medial r:Fibula distal l), ~as.integer(as.character(.)))

But it produces the error: mutat_if is not found (currently there are no packages in use except “reader”)

The next question would be if R can arrange the data in ascending order. It is biological data so we have overlapping and missing data. R should arrange the observations with as little overlap as possible.

Here an example how my Data looks like:

mutate_if() comes from dplyr so you have to load it first.

If you need more specific help, please provide a proper REPRoducible EXample (reprex) illustrating your issue.

1 Like

I would try using library(dplyr) to load the dplyr package where the mutate function "lives." If you haven't used it before you may have to install it. Another way to load the dplyr package would be to do dplyr::mutate_if(), but it will only work for that specific line of code (vs. library() which will make functions from that package available throughout your script).

2 Likes

Thank you! But the code now produces this Error:

mutate_if(is.numeric, ~as.factor(as.character(.)))
Error in UseMethod("tbl_vars") :
no applicable method for 'tbl_vars' applied to an object of class "function"
mutate_at(vars(Clavicula_medial_r:Fibula_distal_l), ~as.integer(as.character(.)))
Error in UseMethod("tbl_vars") :
no applicable method for 'tbl_vars' applied to an object of class "c('quosures', 'list')"

Hey @Der_Blauebaer. Can you share a reproducible example (sometimes called "REPREX") of the problem? Look at the reply by andresrcs for the link to an explanation of how to do that.

this is missing a data param. specifying what data.frame is being mutated

1 Like

Thank you, it was that simpel, just had to add
Auswertung <- Auswertung %>%
mutate_if(is.numeric, ~as.factor(as.character(.)))
to make clear what should be mutatet and whrer to save.

I will try to make a REPREX for the second question. I will start a new Topic for that.
Thank you for your help.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.