[addindividuals] => [{"Drug":"Calpol","Strength":"100mg","Form":"Liquid","Quantity":"1"},{"Drug":"Paracetamol","Strength":"200mg","Form":"Tablet","Quantity":"16"}]
我希望有这样的输出
df <- structure(list(V = structure(c(4L, 5L, 3L, 7L, 6L, 2L, 1L), .Label = c("132 B26,172 B27,107 B57,104 B59,137 B60,133 B61,103 B62,134 B63,177 B100,123 B133,184 B168,109 B197,103 B198,173 B202,157 B203,143 B266,62 B342,62 B354,92 B355,195 B368,164 B370,52 B468,74 B469,71 B484,98 B494,66 B502,63 B601,133 B622",
"135A,510A,511A,60 B23,67 B24,70 B25,95 B26,122 B27,123 B27,109 B60",
"25A,28 B55,31 B56,45 B57,43 B58,5 B59,47 B59,6 B60,69 B60,66 B61",
"267 B361,786 B363,543 B392", "563 B202,983 B360", "8 B1,12 B35,10 B71,9 B154,51 B179",
"91 B26,117 B27,117 B28,102 B29,47 B31,96 B63,78 B64,133 B65,117 B66,121 B66,112 B67,127 B100"
), class = "factor")), .Names = "V", class = "data.frame", row.names = c(NA,
-7L))
我试过一个有效的字符串
V
361, 363, 392
202,360
55,56,57,58,59,59,60,60,61
26,27,28,29,31,63,64,65,66,66,67,100
1,35,71,154,179
23,24,25,26,27,27,60
26,27,57,59,60,61,62,63,100,133,168,197,198,202,203,266,342,354,355,368,370,468,469,484,494,502,601,622
但我不知道如何将它应用于每行中用逗号分隔的所有字符串
答案 0 :(得分:1)
我们可以使用str_extract_all
来获取非数字字符后面的数字。输出将是list
,因此list
sapply
和paste
list
上的元素一起循环toString
paste(., collapse=', ')
)的包装。
library(stringr)
sapply(str_extract_all(df$V, "(?<=[A-Z])\\d+"), toString)
#[1] "361, 363, 392"
#[2] "202, 360"
#[3] "55, 56, 57, 58, 59, 59, 60, 60, 61"
#[4] "26, 27, 28, 29, 31, 63, 64, 65, 66, 66, 67, 100"
#[5] "1, 35, 71, 154, 179"
#[6] "23, 24, 25, 26, 27, 27, 60"
#[7] "26, 27, 57, 59, 60, 61, 62, 63, 100, 133, 168, 197, 198, 202, 203, 266, 342, 354, 355, 368, 370, 468, 469, 484, 494, 502, 601, 622"