我有以下資料集
out
# A tibble: 1,356 x 7
ID GROUP Gender Age Education tests score
<dbl> <chr> <dbl> <dbl> <dbl> <chr> <dbl>
1 1 TRAINING 1 74 18 ADAS_CogT0 14.7
2 1 TRAINING 1 74 18 ROCF_CT0 32
3 1 TRAINING 1 74 18 ROCF_IT0 3.7
4 1 TRAINING 1 74 18 ROCF_RT0 3.9
5 1 TRAINING 1 74 18 PVF_T0 41.3
6 1 TRAINING 1 74 18 SVF_T0 40
7 1 TRAINING 1 74 18 ADAS_CogT7 16
8 1 TRAINING 1 74 18 ROCF_CT7 33
9 1 TRAINING 1 74 18 ROCF_IT7 1.7
10 1 TRAINING 1 74 18 ROCF_RT7 2.4
如果我想創建一個列來代替tests ending with T0對應的值,score0而代替tests ending with T7 the value would be score7`,有哪些可能的方法來實作這一點?
uj5u.com熱心網友回復:
您希望輸出是字串'score0'和'score7'嗎?
你可以試試——
library(dplyr)
out %>%
mutate(result = case_when(grepl('T0$', tests) ~ 'score0',
grepl('T7$', tests) ~ 'score7'))
# ID GROUP Gender Age Education tests score result
#1 1 TRAINING 1 74 18 ADAS_CogT0 14.7 score0
#2 1 TRAINING 1 74 18 ROCF_CT0 32.0 score0
#3 1 TRAINING 1 74 18 ROCF_IT0 3.7 score0
#4 1 TRAINING 1 74 18 ROCF_RT0 3.9 score0
#5 1 TRAINING 1 74 18 PVF_T0 41.3 score0
#6 1 TRAINING 1 74 18 SVF_T0 40.0 score0
#7 1 TRAINING 1 74 18 ADAS_CogT7 16.0 score7
#8 1 TRAINING 1 74 18 ROCF_CT7 33.0 score7
#9 1 TRAINING 1 74 18 ROCF_IT7 1.7 score7
#10 1 TRAINING 1 74 18 ROCF_RT7 2.4 score7
或另一種選擇readr::parse_number。
out %>%
mutate(result = paste0('score', readr::parse_number(tests)))
uj5u.com熱心網友回復:
請把資料放在你的帖子里。>> dput(df)
您可以結合使用 case_when 和 str_detect
library(dplyr)
library(stringr)
df <- structure(
list(
ID = 1:10,
GROUP = rep('TRAINING', 10),
Gender = rep(1, 10),
Education = rep(74, 10),
test = c(
'ADAS_CogT0',
'ROCF_CT0',
'ROCF_IT0',
'ROCF_RT0',
'PVF_T0',
'SVF_T0',
'ADAS_CogT7',
'ROCF_CT7',
'ROCF_IT7',
'ROCF_RT7'
),
score = c(14.7,32,3.7,3.9,41.3,40,16,33,1.7,2.4)
),
row.names = c(1:10),
class = "data.frame"
)
df2 <- df %>%
mutate(new = case_when(str_detect(test, 'T0') ~ 'score0',
str_detect(test, 'T7') ~ 'score7',
TRUE ~ test)
)
ID GROUP Gender Education test score new
1 1 TRAINING 1 74 ADAS_CogT0 14.7 score0
2 2 TRAINING 1 74 ROCF_CT0 32.0 score0
3 3 TRAINING 1 74 ROCF_IT0 3.7 score0
4 4 TRAINING 1 74 ROCF_RT0 3.9 score0
5 5 TRAINING 1 74 PVF_T0 41.3 score0
6 6 TRAINING 1 74 SVF_T0 40.0 score0
7 7 TRAINING 1 74 ADAS_CogT7 16.0 score7
8 8 TRAINING 1 74 ROCF_CT7 33.0 score7
9 9 TRAINING 1 74 ROCF_IT7 1.7 score7
10 10 TRAINING 1 74 ROCF_RT7 2.4 score7
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/369321.html
