首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >R中字符间的数字提取

R中字符间的数字提取
EN

Stack Overflow用户
提问于 2020-01-27 13:08:43
回答 3查看 83关注 0票数 2

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

代码语言:javascript
复制
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]

我试过这样做:

代码语言:javascript
复制
Data$estimate <- sub("\\[.*","",Data$value)

但它只适用于创建变量估计。我在考虑用strsplit,但它也不管用.

你能帮个忙吗?

非常感谢,

N.

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2020-01-27 13:27:52

使用注释中以可复制形式显示的数据,我们可以使用如图所示的separate。如果fill="right"参数中只列出了一个子字段,则lowerupper将使用NAs填充。

代码语言:javascript
复制
library(dplyr)
library(tidyr)
DF %>%
  separate(value, c("value", "lower", "upper", NA), sep = "[^0-9.]+", fill = "right")

备注

代码语言:javascript
复制
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)
票数 5
EN

Stack Overflow用户

发布于 2020-01-27 13:40:03

使用提尔

代码语言:javascript
复制
separate(df, value, c("estimate", "low", "high"), sep = "\\s\\[|-|\\]")

希望这能有所帮助。

票数 0
EN

Stack Overflow用户

发布于 2020-01-27 13:40:50

这是另一种只使用R基的方法

代码语言:javascript
复制
lapply(strsplit(Data$value, "[^[:digit:].]"), function(x) as.numeric(x[x != ""]))
# [[1]]
# [1] 4.9 2.5 8.6
#
# [[2]]
# [1] 5.1 2.7 8.5
#
# [[3]]
# [1] 5.2 2.9 8.5
#
# [[4]]
# [1] 5.4 3.1 8.6
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59931822

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档