从文本字符串中提取最大数量

时间:2015-09-24 15:12:59

标签: r

我试图从数据框中的Number struck列获取最大值。正如您所看到的,某些行具有范围。提前谢谢。

Aircraft    Number struck
B-757-200   2 to 10
B-737-300   1
B-737-300   1
B-727-200   1
UNKNOWN     1
C-550       1
B-727-200   1
CITATION II 1
DA-2000     1
B-737-500   1
B-737-300   2 to 10
UNKNOWN     2 to 10
HAWKER 800  1
MD-80       11 to 100
B-737-400   1
B-737       1
B-767-300   2 to 10
EMB-120     2 to 10

数据

df <- structure(list(Aircraft = c("B-757-200", "B-737-300", "B-737-300", 
"B-727-200", "UNKNOWN", "C-550", "B-727-200", "CITATION II", 
"DA-2000", "B-737-500", "B-737-300", "UNKNOWN", "HAWKER 800", 
"MD-80", "B-737-400", "B-737", "B-767-300", "EMB-120"), Number.struck = c("2 to 10", 
"1", "1", "1", "1", "1", "1", "1", "1", "1", "2 to 10", "2 to 10", 
"1", "11 to 100", "1", "1", "2 to 10", "2 to 10")), .Names = c("Aircraft", 
"Number.struck"), row.names = c(NA, -18L), class = "data.frame")

1 个答案:

答案 0 :(得分:0)

也许这会起作用

res <- as.numeric(as.character(unlist(strsplit(gsub("[a-zA-Z]","",df$Number.struck),"\\s"))))
max(res,na.rm=T)