嗨,有这个数据框
id power hr fr VE VO2 VCO2 PETCO2 percent_VO2 percent_power
1 AC12-PRD-C1 25 88.75 22.75 22.75 0.73900 0.66700 39.2925 49.34068 21.73913
2 AC12-PRD-C1 40 93.25 23.00 23.75 0.81975 0.71500 39.6200 54.73210 34.78261
3 AC12-PRD-C1 55 99.75 22.75 26.75 0.95125 0.85400 41.4100 63.51193 47.82609
4 AC12-PRD-C1 70 109.75 23.00 32.50 1.07525 1.04700 42.0150 71.79102 60.86957
5 AC12-PRD-C1 85 118.75 22.75 39.50 1.19900 1.25125 41.8425 80.05341 73.91304
6 AC12-PRD-C1 100 127.00 26.00 48.25 1.34575 1.51850 41.0950 89.85144 86.95652
7 AC12-PRD-C1 115 135.75 28.00 55.75 1.49775 1.76025 40.7275 100.00000 100.00000
8 AL13-PRD-C1 25 69.50 16.50 24.00 0.66125 0.58050 31.2275 41.36691 19.23077
9 AL13-PRD-C1 40 73.00 17.50 26.50 0.74850 0.66425 32.1025 46.82515 30.76923
10 AL13-PRD-C1 55 83.25 15.50 29.00 0.85500 0.79425 33.6650 53.48764 42.30769
11 AL13-PRD-C1 70 93.75 16.00 36.50 0.98450 0.99925 34.5325 61.58899 53.84615
12 AL13-PRD-C1 85 104.50 16.00 44.75 1.14950 1.23475 34.4225 71.91117 65.38462
13 AL13-PRD-C1 100 114.25 19.25 55.25 1.34650 1.48375 33.1800 84.23522 76.92308
14 AL13-PRD-C1 115 125.25 20.75 63.75 1.45100 1.65775 32.6450 90.77260 88.46154
15 AL13-PRD-C1 130 136.25 24.75 78.00 1.59850 1.89075 30.9000 100.00000 100.00000
16 BM06-PRD-S1 25 119.25 18.25 19.00 0.61675 0.58225 37.6425 48.87084 25.00000
17 BM06-PRD-S1 40 126.00 18.00 20.75 0.71700 0.65950 39.2175 56.81458 40.00000
18 BM06-PRD-S1 55 133.50 20.75 25.00 0.86275 0.82750 41.2150 68.36371 55.00000
19 BM06-PRD-S1 70 147.25 18.25 29.00 0.98575 1.04550 41.7050 78.11014 70.00000
20 BM06-PRD-S1 85 158.50 22.25 39.25 1.13000 1.30525 41.1425 89.54041 85.00000
21 BM06-PRD-S1 100 168.75 27.75 51.00 1.26200 1.61150 38.8925 100.00000 100.00000
22 CB19-PRD-S1 25 98.75 18.50 25.00 0.88350 0.80475 40.7550 36.15715 13.15789
23 CB19-PRD-S1 40 98.25 20.00 25.50 0.94575 0.82900 41.4675 38.70473 21.05263
24 CB19-PRD-S1 55 102.00 19.75 28.50 1.08125 0.95800 42.2775 44.25005 28.94737
25 CB19-PRD-S1 70 107.50 20.50 34.25 1.24400 1.14275 42.6450 50.91058 36.84211
26 CB19-PRD-S1 85 111.00 21.25 35.50 1.30475 1.19925 43.3600 53.39677 44.73684
27 CB19-PRD-S1 100 117.25 21.50 40.25 1.47350 1.42225 44.2650 60.30284 52.63158
28 CB19-PRD-S1 115 123.00 22.75 47.00 1.67900 1.68475 44.6400 68.71291 60.52632
29 CB19-PRD-S1 130 129.50 24.50 52.50 1.79075 1.87950 44.3425 73.28627 68.42105
30 CB19-PRD-S1 145 135.50 25.25 59.50 1.96000 2.13525 44.7300 80.21281 76.31579
31 CB19-PRD-S1 160 145.25 26.75 64.50 2.04050 2.28350 43.8825 83.50726 84.21053
32 CB19-PRD-S1 175 151.25 30.50 83.00 2.34425 2.76050 41.6025 95.93820 92.10526
33 CB19-PRD-S1 190 161.75 33.75 92.25 2.44350 2.96850 40.0400 100.00000 100.00000
我想根据在CHD
列中写入的内容来创建一个具有healthy
或id
值的新列。
如果为“ PRD-C1”,则应为CHD
;如果为“ PRD-S1”,则应为healthy
。
谢谢!
答案 0 :(得分:0)
df$newColumn=ifelse(grepl("PRD-C1",df$id),"CHD","healthy")
如果只有“ PRD-C1”和“ PRD-S1”是两个选项。
答案 1 :(得分:0)
使用dplyr
的解决方案:
library(dplyr)
df %>%
mutate(new_column = ifelse(grepl(".*-PRD-C1", ID), "CHD", "healthy"))
1。创建可复制的最小示例数据:
df <- data.frame(ID = c("AC12-PRD-C1", "CB19-PRD-S1"),
stringsAsFactors = FALSE)
2。使用dplyr
library(dplyr)
df %>%
mutate(new_column = ifelse(grepl(".*-PRD-C1", ID), "CHD", "healthy"))
返回:
ID new_column
1 AC12-PRD-C1 CHD
2 CB19-PRD-S1 healthy