我有一段文字,我想从中提取每个样本大小。我通常可以让Regex正常工作,但我无法。
这是一个例子:
x = "OBJECTIVES:
In diabetic polyneuropathy (DPN) patients, the effect of folic acid and homocysteine has been related to components of nerve conduction velocity (NCV). The objective of this study was to determine the effect of folic acid supplementation on NCV in DPN patients.
METHODS:
Patients were randomized to receive either 1 mg of folic acid (n = 40) or placebo (n = 40) for 16 weeks. Blood samples were collected to assess serum folic acid and homocysteine concentrations, and NCV was performed for assessment of diabetic neuropathy.
RESULTS:
At 16 weeks, in the supplemented group, serum levels of folic acid (p < 0.001) increased, homocysteine concentrations decreased (p < 0.001), with no change in serum vitamin B12 levels. There was a significant increase in sensory sural amplitude (p < 0.001), and components of motor nerves, including amplitude (p = 0.001) and velocity (p < 0.001), but decreased onset latency of peroneal (p = 0.019) and tibial (p = 0.011) motor nerves.
CONCLUSION:
Our data suggest that supplementation with 1 mg of folic acid for 16 weeks may be useful for enhancing NCV in DPN patients."
我想提取两个样本大小。在这种情况下,n = 40,n = 40。
我试过了
gsub('.*[n=]|).*','',x)
我回来 ts.
您可以stringr
提取“ n =”后接至少一位数字。假设等号的两边都没有空格或一个空格:
library(stringr)
str_extract_all(x, "n\\s?\\=\\s?\\d+")
[[1]]
[1] "n = 40" "n = 40"
编辑:以下应mutate
与您的其他条件一起使用。我从切换到stringr
,stringi
以获取NA
没有匹配项的字符串。另外,您可以使用paste
代替,stri_flatten
但我坚持使用,stri_flatten
因为它保留NA
为缺少的值,而不是"NA"
像字符一样的字符paste
。
sapply(stri_extract_all(x, regex = "n\\s?\\=\\s?\\d+"), stri_flatten, collapse = ", ")
对于正则表达式,我从R的备忘单开始(仍然参考它)。上面的正则表达式的工作原理如下:
n
-字母n
\\s?
-最多1个(the ?
)space(\\s
)(*
与?
--your调用相比,您可能更喜欢MrFlick的使用)
\\=
-等号
\\s?
-最多1(the ?
)个空格(\\s
)
\\d+
-一个或多个(+
)个数字(\\d
)
资料:
x = c("OBJECTIVES:
In diabetic polyneuropathy (DPN) patients, the effect of folic acid and homocysteine has been related to components of nerve conduction velocity (NCV). The objective of this study was to determine the effect of folic acid supplementation on NCV in DPN patients.
METHODS:
Patients were randomized to receive either 1 mg of folic acid (n = 40) or placebo (n = 40) for 16 weeks. Blood samples were collected to assess serum folic acid and homocysteine concentrations, and NCV was performed for assessment of diabetic neuropathy.
RESULTS:
At 16 weeks, in the supplemented group, serum levels of folic acid (p < 0.001) increased, homocysteine concentrations decreased (p < 0.001), with no change in serum vitamin B12 levels. There was a significant increase in sensory sural amplitude (p < 0.001), and components of motor nerves, including amplitude (p = 0.001) and velocity (p < 0.001), but decreased onset latency of peroneal (p = 0.019) and tibial (p = 0.011) motor nerves.
CONCLUSION:
Our data suggest that supplementation with 1 mg of folic acid for 16 weeks may be useful for enhancing NCV in DPN patients.", "no numbers here", "n = 100")
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句