r - 提取R中字符之间的数字

标签 r

我需要将以下数据集中的“值”变量分成三个变量:估计、低、高。请注意,有时没有置信区间,所以我只有值。

country gho year    publishstate    value
Afghanistan Raised fasting blood glucose (>=7.0 mmol/L or on medication)(age-standardized estimate) 1980    Published   4.9 [2.5-8.6]
Afghanistan Raised fasting blood glucose (>=7.0 mmol/L or on medication)(age-standardized estimate) 1981    Published   5.1 [2.7-8.5]
Afghanistan Raised fasting blood glucose (>=7.0 mmol/L or on medication)(age-standardized estimate) 1982    Published   5.2 [2.9-8.5]
Afghanistan Raised fasting blood glucose (>=7.0 mmol/L or on medication)(age-standardized estimate) 1983    Published   5.4 [3.1-8.6]

我已经尝试过这个:

Data$estimate <- sub("\\[.*","",Data$value)

但它仅适用于创建变量估计。我正在考虑使用 strsplit 但它也不起作用......

你能帮忙解决一下吗?

非常感谢,

N.

最佳答案

使用注释中显示的可重复形式的数据,我们可以使用如图所示的单独。如果 value< 中仅列出一个子字段,则 fill="right" 参数会导致 lowerupper 用 NA 填充.

library(dplyr)
library(tidyr)
DF %>%
  separate(value, c("value", "lower", "upper", NA), sep = "[^0-9.]+", fill = "right")

注意

Lines <- "country,glucose,year,publishstate,value
Afghanistan,Raised fasting blood glucose (>=7.0 mmol/L or on medication)(age-standardized estimate),1980,Published,4.9 [2.5-8.6]
Afghanistan,Raised fasting blood glucose (>=7.0 mmol/L or on medication)(age-standardized estimate),1981,Published,5.1 [2.7-8.5]
Afghanistan,Raised fasting blood glucose (>=7.0 mmol/L or on medication)(age-standardized estimate),1982,Published,5.2 [2.9-8.5]
Afghanistan,Raised fasting blood glucose (>=7.0 mmol/L or on medication)(age-standardized estimate),1983,Published,5.4 [3.1-8.6]"
DF <- read.csv(text = Lines, header = TRUE, as.is = TRUE)

关于r - 提取R中字符之间的数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59931822/

相关文章:

r - 计算唯一组合并在新列中汇总其他列

R:lubridate 返回 NA – 意外地

r - 两个 sf 对象的空间合并

r - 修复ggplot2中网格的宽度

r - R 中的数据框不重复类别

r - 使用 ggplot 在同一直方图中绘制两个变量

r - 从 Bootstrap 显示密度图

r - 如何处理R中矩阵的无穷大?

r - data.table 使用新名称连接(多个)选定的列

r - 如何根据facet_wrap所有方面的值限制x轴长度