Don't know the r codes

Hello, not sure if this is the right place to ask, but I am quite desperate. I have an assignment due and I don't know some of the required r codes!! I just have two questions.

  1. I have a data file that has a variable named confident. This variable can have a response ranging from strongly disagree, moderately disagree, neither disagree nor disagree, moderately agree, and strongly agree. These responses are also given points (i.e. 1 point for strongly disagree, 2 points for moderately disagree, 3 points for neither disagree nor disagree and so on). I have many participants in a study that responded to the given response option. The problem is, I need one complete set of r code to create a new variable called confidentpoints which assigns points to each response for the variable. Note that I need to both change the name of the variable and be able to assign points for each response.
  2. What would be the modification to the code from #1 that I would use to assign points to a reverse coded statement of confident (e.g. shy, thus 5 points for strongly disagree, 4 points for strongly disagree, and so on). Note that I don't need to change the variable name this time, just how I would assign points to a reverse coded statement.

So I dummied some data up (for future reference, it is very helpful if you do this yourself — we often call this a "reprex"— see link below my code). I didn't follow the exact parameters for your assignment, which is good, since you'll get a chance to play around with it!

I think that working with the forcats package will make your life much easier:

Below I've used fct_inorder(), for example, to make sure that the factor levels were sequential.

To reverse the order of levels, you can use fct_rev(), which I don't do below.

suppressPackageStartupMessages(library(tidyverse))
mydata <- tibble::tribble(
   ~subject,                   ~confident,
    "Alice",             "strongly agree",
      "Bob", "neither agree nor disagree",
  "Cynthia",        "moderately disagree",
    "Dylan",           "moderately agree",
     "Eric",          "strongly disagree",
    "Frank",           "moderately agree",
     "Gina",             "strongly agree"
  )

glimpse(mydata)
#> Observations: 7
#> Variables: 2
#> $ subject   <chr> "Alice", "Bob", "Cynthia", "Dylan", "Eric", "Frank", "…
#> $ confident <chr> "strongly agree", "neither agree nor disagree", "moder…

# currently both are characters, we want `confident` to be a factor

library(forcats)

mylevels <- factor(c("strongly disagree", "moderately disagree", 
                "neither agree nor disagree", "moderately agree",
                "strongly agree"))

mylevels <- fct_inorder(mylevels, ordered = TRUE)

mydata <- mydata %>%
  mutate("confident" = factor(confident, levels = mylevels))


glimpse(mydata)
#> Observations: 7
#> Variables: 2
#> $ subject   <chr> "Alice", "Bob", "Cynthia", "Dylan", "Eric", "Frank", "…
#> $ confident <fct> strongly agree, neither agree nor disagree, moderately…

mydata <- mydata %>%
  mutate(confident_num = as.integer(confident))

mydata
#> # A tibble: 7 x 3
#>   subject confident                  confident_num
#>   <chr>   <fct>                              <int>
#> 1 Alice   strongly agree                         5
#> 2 Bob     neither agree nor disagree             3
#> 3 Cynthia moderately disagree                    2
#> 4 Dylan   moderately agree                       4
#> 5 Eric    strongly disagree                      1
#> 6 Frank   moderately agree                       4
#> 7 Gina    strongly agree                         5

Created on 2019-03-01 by the reprex package (v0.2.1)

There's a nice "getting started" guide to forcats here:
https://forcats.tidyverse.org/articles/forcats.html

1 Like

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.