How to transform Likert data into another form ?

Hi,

I would like to go from that shape (first example) of the Likert data:

df_likert <- structure(list(Sex = c(1L, 2L, 1L, 2L), `Age Group` = structure(c(1L,
                                                                        1L, 2L, 2L),
 .Label = c("30-39", "40-49"), class = "factor"),
                     Q1 = c(1L, 3L, 4L, 2L), Q2 = c(7L, 5L, 6L, 2L), Q3 = 1:4,
                     Q4 = c(5L, 6L, 2L, 2L)), class = "data.frame", row.names = c(NA,
                                                                                   -4L))

and that form (second example):

q1<-c(2,2,3,3,3,4,4,4,5,5)

q2<-c(2,3,3,4,4,4,4,5,5,5)

q3<-c(2,2,2,3,4,4,4,5,5,5)

q4<-c(2,2,2,3,4,4,4,5,5,5)

q5<-c(2,2,2,3,4,4,4,5,5,5)

df_likert2 <-data.frame(q1,q2,q3, q4, q5)

into that form (final form):

Year1_likert_exhibits <- structure(list(Item = c("Were informative and educational", "Were easy to read", 
"Covered topics I liked", "Were introduced in an informative way by staff", 
"Were informative and educational", "Were easy to read", "Covered topics I liked", 
"Were introduced in an informative way by staff", "Were informative and educational", 
"Were easy to read", "Covered topics I liked", "Were introduced in an informative way by staff", 
"Were informative and educational", "Were easy to read", "Covered topics I liked", 
"Were introduced in an informative way by staff", "Were informative and educational", 
"Were easy to read", "Covered topics I liked", "Were introduced in an informative way by staff"
), Evaluation = c("Strongly agree", "Strongly agree", "Strongly agree", 
"Strongly agree", "Somewhat agree", "Somewhat agree", "Somewhat agree", 
"Somewhat agree", "Neither agree nor disagree", "Neither agree nor disagree", 
"Neither agree nor disagree", "Neither agree nor disagree", "Somewhat disagree", 
"Somewhat disagree", "Somewhat disagree", "Somewhat disagree", 
"Strongly disagree", "Strongly disagree", "Strongly disagree", 
"Strongly disagree"), Value = c(135, 136, 134, 122, 24, 21, 24, 
21, 3, 2, 4, 16, 2, 3, 1, 2, 4, 4, 4, 4), percent = c(20, 20, 
20, 18, 4, 3, 4, 3, 0, 0, 1, 2, 0, 0, 0, 0, 1, 1, 1, 1), level = c("5", 
"5", "5", "5", "4", "4", "4", "4", "3", "3", "3", "3", "2", "2", 
"2", "2", "1", "1", "1", "1")), row.names = c(NA, -20L), class = c("tbl_df", 
"tbl", "data.frame"))

The level of coding is as usual with Likert scale and presented below as follows:

5 - Strongly agree,  4 - Somewhat agree,  3  -   Neither agree nor disagree,  
2 -   Somewhat disagree, 1  -  Strongly disagree

Obviously "Year1_likert_exhibits " is a different data and I have included it in here as an example to which I would like to get at the end. I am trying and struggle a bit so I returned here for help regarding that task. Thank you in advance for any guidance.

I have got another dataframe to practice with:

des3x <- structure(list(
  DES1 = c(3, 2, 2, 1, 1, 1, 2, 1, 1, 4), DES2 = c(
    4,     3, 2, 1, 2, 1, 1, 2, 4, 2
  ), DES3 = c(    4, 2, 3, 1, 2, 1, 1, 1,
    3, 3  ), DES4 = c(2, 1, 2, 2, 2, 1, 4, 1, 1, 3), DES5 = c(
    2, 2,    2, 1, 4, 1, 3, 2, 3, 0
  ), DES6 = c(    4, 3, 3, 1, 3, 2, 3, 2, 3,
    2  ), DES7 = c(4, 1, 3, 2, 1, 1, 1, 0, 4, 0), DES8 = c(
    1, 0, 1,    0, 2, 0, 0, 0, 0, 0
  ), DES9 = c(1, 0, 1, 0, 1, 1, 0, 0, 2, 0),
  DES10 = c(4, 1, 2, 2, 1, 2, 4, 1, 2, 4)
), row.names = c(  NA,  -10L
), class = c("tbl_df", "tbl", "data.frame"))

I would like to get it to the following shape:

Any ideas please will be greatly appreciated, thank you.

f(x) = y

x = des3x, a found data frame containing integers representing a variable to be identified to y as Level , Evaluation in variables named DES1 \dots DES10 the names of which correspond to a character vector to be named Item to be created. If des3x does not already exist, it should be created with survey::likert() to eliminate steps.

Example:

library(survey)
data(pisaitems)
items29 <- pisaitems[,substr(names(pisaitems), 1,5) == 'ST25Q']
names(items29) <- c("Magazines", "Comic books", "Fiction", 
                    "Non-fiction books", "Newspapers")
(l29 <- likert(items29))
summary(l29)
plot(l29)

y = result, a data frame with variables Item and Level to be taken from x and Item, and Evaluation, Value and Percent to be calculated from Item and Level.

f is a composed object to apply functions to x and Item, which may be expressed as a chained object—f_1() |> f_2() \dots |> f_n

Begin by creating Item

 Items <- c("Who","What","Where","When","How","Which","Whence","Wither","Thither","Hither")

Compose f1()

colnames(des3x) <- Items

\dots to be continued

I got this from experienced friend and still studying it as I do not know if this is correct because I spotted difference in comparison to the template dataframe:

des3x <- structure(list(
  DES1 = c(3, 2, 2, 1, 1, 1, 2, 1, 1, 4), DES2 = c(
    4, 3, 2, 1, 2, 1, 1, 2, 4, 2
  ), DES3 = c(
    4, 2, 3, 1, 2, 1, 1, 1,
    3, 3
  ), DES4 = c(2, 1, 2, 2, 2, 1, 4, 1, 1, 3), DES5 = c(
    2, 2, 2, 1, 4, 1, 3, 2, 3, 0
  ), DES6 = c(
    4, 3, 3, 1, 3, 2, 3, 2, 3,
    2
  ), DES7 = c(4, 1, 3, 2, 1, 1, 1, 0, 4, 0), DES8 = c(
    1, 0, 1, 0, 2, 0, 0, 0, 0, 0
  ), DES9 = c(1, 0, 1, 0, 1, 1, 0, 0, 2, 0),
  DES10 = c(4, 1, 2, 2, 1, 2, 4, 1, 2, 4)
), row.names = c(NA, -10L), class = c("tbl_df", "tbl", "data.frame"))


lut <- tibble(
  value = c(0:4),
  Evaluation = c(
    "not_applicable",
    "not_stressful",
    "slightly_stressful",
    "moderately_stressful",
    "very_stressful")
) |>  mutate(Evaluation = fct_reorder(Evaluation, value))

des3x_final <- des3x %>%
  pivot_longer(everything()) %>%
  left_join(lut, by = "value")

des3x_final <- des3x_final %>% group_by(name) %>% filter (! duplicated(Evaluation)) %>% ungroup()
#removing duplicates

des3x_final <- des3x_final %>% mutate(Percents = round(value/sum(value)*100, digits = 2))
 %>% relocate(Evaluation, .after=Percents)

des3x_final <- des3x_final %>% mutate(name = factor(name, levels = paste("DES", 1:10, sep = ""))) 
 #for better sorting

Comparing to template dataframe, there are higher values (like122, 135,136, etc.) in Value column up there,
in mine dataframe looks like there just are single values ? Please correct me if I am wrong.

I checked with SPSS and I should have different results in Value and Percent columns:

For example for DES1 question it should be like this:
obraz

but instead I do have single values (instead of calculated all answers) in my des3x_final dataframe. How can I correct it ?

obraz

Unfortunately SPSS can't do it in one table but separates data into single resulting tables according to questions, so from DES1 to DES10 I have received 10 separate tables. I want to have it in one dataframe. How do I do it ?

1 Like

This topic was automatically closed 42 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.