我有下表:
| ID | 行動 | 樂趣 | 歷史 | 用處 | 意圖 |
|---|---|---|---|---|---|
| 一種 | 4 | 5 | 1 | 非常同意 | 同意 |
| 乙 | 5 | 3 | 4 | 同意 | 同意 |
| C | 3 | 3 | 4 | 同意 | 不同意 |
| d | 4 | 3 | 5 | 中性的 | 同意 |
| 電子 | 1 | 3 | 4 | 同意 | 同意 |
現在我想將每一行的動作,fun或history高于 3 的每一行都設為一行。這意味著,如果action,fun或者history是所有上述3,它應該是三行。帶有 ID 的條目a應該是兩行(action并且fun在 3 之上,history在 3 之下)并且如下所示:
| ID | 型別 | 用處 | 意圖 |
|---|---|---|---|
| 一種 | 行動 | 非常同意 | 同意 |
| 一種 | 樂趣 | 非常同意 | 同意 |
| 乙 | 樂趣 | 同意 | 同意 |
| 乙 | 歷史 | 同意 | 同意 |
| C | 歷史 | 同意 | 不同意 |
最后,我想要一個像這樣的李克特圖:

同type為汽車品牌和兩組(usefulness, intention)。
uj5u.com熱心網友回復:
一tidyr混合溶液;
library(tidyr)
library(dplyr)
df %>%
pivot_longer(cols = c("action", "fun", "history"), names_to = "type") %>%
filter(value > 3) %>%
select(-value) %>%
relocate(type, .after = id)
輸出;
id type usefulness intention
<chr> <chr> <chr> <chr>
1 a action Strongly Agree Agree
2 a fun Strongly Agree Agree
3 b action Agree Agree
4 b history Agree Agree
5 c history Agree Disagree
6 d action Neutral Agree
7 d history Neutral Agree
8 e history Agree Agree
資料;
df <- structure(list(id = c("a", "b", "c", "d", "e"), action = c(4L,
5L, 3L, 4L, 1L), fun = c(5L, 3L, 3L, 3L, 3L), history = c(1L,
4L, 4L, 5L, 4L), usefulness = c("Strongly Agree", "Agree", "Agree",
"Neutral", "Agree"), intention = c("Agree", "Agree", "Disagree",
"Agree", "Agree")), class = "data.frame", row.names = c(NA, -5L
))
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/333563.html
