我有兩個變數。一種分類標簽,其標簽取值 1-7。值 7 表示“其他” 其他變數是表示“其他”的文本。
col1 col2
1
3
6
2
7 "some text"
7 "some text"
2
7 "some other text"
7 "some third text"
7 "Some thrid Text"
我想根據 col2 中的值更改 col1 中的值。所以例如
IF col2 = "some text", col1 = 2 (and it deletes the "some text" in col2)
IF col2 = "some other text", col1 = 4 (and it deletes the "some other text in col2)
IF col2 = "Some thrid Text", col2 = "some third text"
col1 col2
1
3
6
2
2
2
2
4
7 "some third text"
7 "some third Text"
旁注: col1 在它的值 1-7 上有值標簽,我不想弄亂它。
uj5u.com熱心網友回復:
df %>%
mutate(col1 = coalesce(case_when(col2 == 'some text' ~ 2,
col2 == 'some other text' ~ 4), col1),
col2 = str_remove(col2, 'some (other )?text'))
col1 col2
1 1
2 3
3 6
4 2
5 2
6 2
7 2
8 4
9 7 some third text
10 7 Some thrid Text
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/435616.html
上一篇:jsvue如何添加更多變數
