我想在相同的高度大小间隔内选择高度大小间隔(height >0.50
和height < 1.00
)和模式中的两个级别(例如:norm
和esti
)。例:
sp diameter height breaking pattern
actcon 15,10 9,50 no norm
actcon 8,90 9,46 no norm
actcon 7,00 9,40 no norm
actcon 12,50 9,25 no norm
actcon 8,60 9,00 no norm
actcon 7,70 8,76 no norm
actcon 0,80 0,50 yes norm
actcon 0,50 0,50 no norm
actcon 0,90 0,53 yes norm
actcon 0,55 0,54 no norm
actcon 0,65 0,54 no norm
actcon 1,10 0,50 no curv
actcon 0,85 0,93 no norm
actcon 1,20 0,94 no norm
actcon 1,30 0,94 no deit
actcon 0,90 0,94 no norm
actcon 2,10 0,94 yes norm
actcon 1,00 0,95 no norm
actcon 0,90 0,95 no norm
actcon 0,80 0,95 no norm
actcon 1,00 0,95 no norm
actcon 1,05 0,96 no norm
actcon 1,00 0,96 no norm
actcon 0,90 1,30 no esti
[我看到@akrun在评论时添加了相同的解决方案,因为我写这篇文章]
你要,
subdf <- subset(yourdf, subset = (height >0.50 & height < 1.00) &
pattern %in% c("norm","esti"))
这使
> subdf
sp diameter height breaking pattern
9 actcon 0.90 0.53 yes norm
10 actcon 0.55 0.54 no norm
11 actcon 0.65 0.54 no norm
13 actcon 0.85 0.93 no norm
14 actcon 1.20 0.94 no norm
16 actcon 0.90 0.94 no norm
17 actcon 2.10 0.94 yes norm
18 actcon 1.00 0.95 no norm
19 actcon 0.90 0.95 no norm
20 actcon 0.80 0.95 no norm
21 actcon 1.00 0.95 no norm
22 actcon 1.05 0.96 no norm
23 actcon 1.00 0.96 no norm
如果要删除由于子集而不再存在的因子级别,
> str(subdf)
'data.frame': 13 obs. of 5 variables:
$ sp : Factor w/ 1 level "actcon": 1 1 1 1 1 1 1 1 1 1 ...
$ diameter: num 0.9 0.55 0.65 0.85 1.2 0.9 2.1 1 0.9 0.8 ...
$ height : num 0.53 0.54 0.54 0.93 0.94 0.94 0.94 0.95 0.95 0.95 ...
$ breaking: Factor w/ 2 levels "no","yes": 2 1 1 1 1 1 2 1 1 1 ...
$ pattern : Factor w/ 4 levels "curv","deit",..: 4 4 4 4 4 4 4 4 4 4 ...
那你可以做
subdf <- droplevels(subdf)
> str(subdf)
'data.frame': 13 obs. of 5 variables:
$ sp : Factor w/ 1 level "actcon": 1 1 1 1 1 1 1 1 1 1 ...
$ diameter: num 0.9 0.55 0.65 0.85 1.2 0.9 2.1 1 0.9 0.8 ...
$ height : num 0.53 0.54 0.54 0.93 0.94 0.94 0.94 0.95 0.95 0.95 ...
$ breaking: Factor w/ 2 levels "no","yes": 2 1 1 1 1 1 2 1 1 1 ...
$ pattern : Factor w/ 1 level "norm": 1 1 1 1 1 1 1 1 1 1 ...
但这可能不是正确的事情,取决于你的实际问题。
您也可以下载“dplyr”软件包,并使用或运算符|使用过滤器功能。通常使用dplyr可以更简单地清理数据。
install.packages("dplyr")
library(dplyr)
filter(subdf, height>.5|height<1,pattern=="norm"|pattern=="esti")
此代码将subdf指定为数据框,表示高度可以大于5或小于1,其中模式可以是“norm”或“esti”。如果您想继续使用此子集,则必须将其分配给其他内容。这不会改变您的原始数据。