0

I have this data with Five column names: ID, Q1, Q2, Q3, Q4, and Q5.

ID Q1 Q2 Q3 Q4
101 (2) important (3) very important (1) No imporant (1) No imporant
102 (3) very important (2) important (2) important (3) very important
103 (1) No imporant (1) No imporant (3) very important (2) important
104 (2) important (3) very important (1) No imporant (1) No imporant
105 (3) very important (2) important (2) important (3) very important
106 (1) No imporant (1) No imporant (3) very important (2) important
107 (2) important (3) very important (1) No imporant (1) No imporant
108 (3) very important (2) important (2) important (3) very important
109 (1) No imporant (1) No imporant (3) very important (2) important
110 (2) important (3) very important (1) No imporant (1) No imporant

I want to convert it to this:

ID Q1 Q2 Q3 Q4
101 2 3 1 1
102 3 2 2 3
103 1 1 3 2
104 2 3 1 1
105 3 2 2 3
106 1 1 3 2
107 2 3 1 1
108 3 2 2 3
109 1 1 3 2
110 2 3 1 1
Martin Gal
  • 16,640
  • 5
  • 21
  • 39
  • 2
    Please make a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) or [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) with a sample input (not posted in an image) and your expected output. This is needed to create, test and verify possible solutions. – Martin Gal Mar 28 '22 at 19:00

2 Answers2

3

If your dataset is data, you can do this, using dplyr::mutate(across()) and stringr::str_extract()

library(dplyr)
library(stringr)

data %>% 
  mutate(across(starts_with("Q"),~str_extract(.x,"\\d")))

Output:

      ID     Q1     Q2     Q3     Q4
   <num> <char> <char> <char> <char>
1:   101      2      3      1      1
2:   102      3      2      2      3
3:   103      1      1      3      2
langtang
  • 22,248
  • 1
  • 12
  • 27
2

This is a perfect use case for parse_number from readr package:

library(readr)
library(dplyr)

df %>% 
  mutate(across(-ID, ~parse_number(.)))
    ID Q1 Q2 Q3 Q4
1  101  2  3  1  1
2  102  3  2  2  3
3  103  1  1  3  2
4  104  2  3  1  1
5  105  3  2  2  3
6  106  1  1  3  2
7  107  2  3  1  1
8  108  3  2  2  3
9  109  1  1  3  2
10 110  2  3  1  1

data:

structure(list(ID = 101:110, Q1 = c("(2) important", "(3) very important", 
"(1) No imporant", "(2) important", "(3) very important", "(1) No imporant", 
"(2) important", "(3) very important", "(1) No imporant", "(2) important"
), Q2 = c("(3) very important", "(2) important", "(1) No imporant", 
"(3) very important", "(2) important", "(1) No imporant", "(3) very important", 
"(2) important", "(1) No imporant", "(3) very important"), Q3 = c("(1) No imporant", 
"(2) important", "(3) very important", "(1) No imporant", "(2) important", 
"(3) very important", "(1) No imporant", "(2) important", "(3) very important", 
"(1) No imporant"), Q4 = c("(1) No imporant", "(3) very important", 
"(2) important", "(1) No imporant", "(3) very important", "(2) important", 
"(1) No imporant", "(3) very important", "(2) important", "(1) No imporant"
)), row.names = c(NA, -10L), class = "data.frame")
TarJae
  • 72,363
  • 6
  • 19
  • 66