2016-10-21 117 views
1

返回的数字我有削减的表像间隔:研究 - 从切

bin targets casos prop phyp  logit 
(-2,-1]  193 6144 0.0314 0 -3.4286244 
    (-1,3]  128 431 0.2970 1 -0.8617025 
(3,11]  137 245 0.5592 1 0.2378497 

我想要得到原来的削减。我试着用:

a<-strsplit(as.character(pl$table[,'bin']), ' ') 

然后我想每一行与分裂:

lapply(a, function(x) strsplit(x, ",")[1]) 

但我没有得到预期的结果,这就是:

(-1,3,11) 

有更好的方法来实现这一目标?我还需要做些什么来获得结果?

感谢。

+1

'不公开(lapply(strsplit(DF $斌, “”),函数(x)的最大值( as.numeric(gsub(“\\(|]”,“”,x))))' – ytk

回答

1

在你的例子中,有比你所希望检索的更多的边界。这会给你所有的界限:

d <- read.table(text=' bin targets casos prop phyp  logit 
"(-2,-1]"  193 6144 0.0314 0 -3.4286244 
    "(1,3]"  128 431 0.2970 1 -0.8617025 
"(3,11]"  137 245 0.5592 1 0.2378497', header=T) 

strings <- as.character(levels(d$bin)) 
strings <- substr(strings, 2, nchar(strings)-1) 
unique(unlist(strsplit(strings, ","))) 
# [1] "-2" "-1" "1" "3" "11" 

如果你只是想的上限,这将工作:

strings <- as.character(levels(d$bin)) 
strings <- sapply(strsplit(strings, ","), function(l){ l[2] }) 
strings <- substr(strings, 1, nchar(strings)-1) 
unique(strings) 
# [1] "-1" "3" "11" 
+0

谢谢,我意识到这一点并在示例中对其进行了更改。 – GabyLP

+0

不客气,@GabyLP。 – gung

3

如果你的数据是一致的这种格式,你可以使用gsub()

df <- data.frame(bin = c('(-2,-1]','(1,3]','(3,11]'), 
       targets = c(193, 128, 137), 
       casos = c(6144, 431, 245), 
       prop = c(0.0314, 0.297, 0.5592), 
       phyp = c(0,1,1), 
       logit = c(-3.4286244,-0.8617025, 0.2378497), stringsAsFactors = F) 

a <- strsplit(df$bin, ',') 
sapply(a, function(x) gsub("]", "", x))[2,] 
sapply(a, function(x) gsub("\\(", "", x))[1,] 

,让你

[1] "-1" "3" "11" 
[1] "-2" "1" "3" 
1

另一种方法是:

a<-strsplit(as.character(pl$table[,'bin']), ' ') 
lapply(a, function(x) unlist(strsplit(x, ",|]"))[2])