评估字符串和空单元格
Evaluating character strings and empty cells
我有一个数据 table,其中一列由缺失的单元格和字符串组成,例如 7 1/4 INCHES
、1/4 INC
、9/16 INCH
、1 3/4 INCHES
、1 13/16 INCHES
、20 INCHES
。我想去掉空格和 INC/INCH/INCHES (字符串拆分)并通过将字符串的其余部分转换为 7+1/4=7.25
.
之类的数字来评估字符串的其余部分
library(data.table)
data<-data.table(variable = c("", "", "7 1/4 INCHES", "1/4 INC", "9/16 INCH", "1 3/4 INCHES", "", "1 13/16 INCHES", "20 INCHES", "", ""))
#Assigning 0s to empty cells
data$variable[data$variable == "" ] = 0
#Getting rid of INCH, INCHES and INCH
data$variable<-gsub("[[:space:]][A-z]*$", "", data$variable)
#Adding "+" instead of whitespace (for summation), like 7+1/4 instead of 7 1/4
data$variable<-gsub( " ", "+", data$variable)
data$variable<-eval(parse(text=data$variable))
但是,我无法使 eval
功能正常工作。你能帮我一下吗?
其次,这个特定的代码似乎不是一种非常有效的方法。我有一个非常大的数据集,它有 4 列,有很多观察结果,就像上面的小例子一样。我怎样才能把东西系紧一点?
编辑:
data$variable<-sapply(data$variable, function(x) eval(parse(text=x)))
我使用上面的代码让它工作。但是,这仍然不是一种有效的方法。
执行此操作的一种方法是将字符串的每个部分提取到单独的变量中,然后使用它们来计算结果。
library(tidyverse)
data %>%
as_tibble() %>%
extract(variable, c("x"), "^(\d+) ", remove = FALSE) %>%
extract(variable, c("y", "z"), "(\d+)/(\d+)", remove = FALSE) %>%
mutate_at(vars(x, y, z), as.numeric) %>%
mutate(result = if_else(is.na(x), 0, x) + if_else(is.na(y / z), 0, y / z)) %>%
select(variable, result)
#> # A tibble: 11 x 2
#> variable result
#> <chr> <dbl>
#> 1 0.0000
#> 2 0.0000
#> 3 7 1/4 INCHES 7.2500
#> 4 1/4 INC 0.2500
#> 5 9/16 INCH 0.5625
#> 6 1 3/4 INCHES 1.7500
#> 7 0.0000
#> 8 1 13/16 INCHES 1.8125
#> 9 20 INCHES 20.0000
#> 10 0.0000
#> 11 0.0000
此 还展示了解决此类问题的几种方法
我有一个数据 table,其中一列由缺失的单元格和字符串组成,例如 7 1/4 INCHES
、1/4 INC
、9/16 INCH
、1 3/4 INCHES
、1 13/16 INCHES
、20 INCHES
。我想去掉空格和 INC/INCH/INCHES (字符串拆分)并通过将字符串的其余部分转换为 7+1/4=7.25
.
library(data.table)
data<-data.table(variable = c("", "", "7 1/4 INCHES", "1/4 INC", "9/16 INCH", "1 3/4 INCHES", "", "1 13/16 INCHES", "20 INCHES", "", ""))
#Assigning 0s to empty cells
data$variable[data$variable == "" ] = 0
#Getting rid of INCH, INCHES and INCH
data$variable<-gsub("[[:space:]][A-z]*$", "", data$variable)
#Adding "+" instead of whitespace (for summation), like 7+1/4 instead of 7 1/4
data$variable<-gsub( " ", "+", data$variable)
data$variable<-eval(parse(text=data$variable))
但是,我无法使 eval
功能正常工作。你能帮我一下吗?
其次,这个特定的代码似乎不是一种非常有效的方法。我有一个非常大的数据集,它有 4 列,有很多观察结果,就像上面的小例子一样。我怎样才能把东西系紧一点?
编辑:
data$variable<-sapply(data$variable, function(x) eval(parse(text=x)))
我使用上面的代码让它工作。但是,这仍然不是一种有效的方法。
执行此操作的一种方法是将字符串的每个部分提取到单独的变量中,然后使用它们来计算结果。
library(tidyverse)
data %>%
as_tibble() %>%
extract(variable, c("x"), "^(\d+) ", remove = FALSE) %>%
extract(variable, c("y", "z"), "(\d+)/(\d+)", remove = FALSE) %>%
mutate_at(vars(x, y, z), as.numeric) %>%
mutate(result = if_else(is.na(x), 0, x) + if_else(is.na(y / z), 0, y / z)) %>%
select(variable, result)
#> # A tibble: 11 x 2
#> variable result
#> <chr> <dbl>
#> 1 0.0000
#> 2 0.0000
#> 3 7 1/4 INCHES 7.2500
#> 4 1/4 INC 0.2500
#> 5 9/16 INCH 0.5625
#> 6 1 3/4 INCHES 1.7500
#> 7 0.0000
#> 8 1 13/16 INCHES 1.8125
#> 9 20 INCHES 20.0000
#> 10 0.0000
#> 11 0.0000
此