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 |
CodePudding user response:
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
CodePudding user response:
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")