如果在给定的时间间隔内

时间:2017-09-19 09:06:01

标签: r database

我有一个包含3个变量的数据库:

Var1 Var2 var3

A 10 20

B 2 20

C -3 20

D 1 20

如果为-1,我想要一个删除(省略)任何观察的通用命令

任何帮助?

1 个答案:

答案 0 :(得分:0)

那么一行代表观察? 或者对每个变量分开?

然后,对于var2,你可以做

Var2 = Var2[-which(Var2==-1)]

当然,其他人也一样。

如果您想在整个数据库上执行此操作:

> var1 = c("A","B","C","D")
> var2 = c(10,2,-3,1)
> var3 = c(20,20,20,20)
> database = cbind(var1,var2,var3)
> 
> database
     var1 var2 var3
[1,] "A"  "10" "20"
[2,] "B"  "2"  "20"
[3,] "C"  "-3" "20"
[4,] "D"  "1"  "20"
> removeRow = which(database==-1, arr.ind=T)[,1]
> if(length(removeRow)>0){ 
+     database = database[-removeRow,]  
+ }
> 
> database
     var1 var2 var3
[1,] "A"  "10" "20"
[2,] "B"  "2"  "20"
[3,] "C"  "-3" "20"
[4,] "D"  "1"  "20"
> 
> #I include an example with -3 as you have no data equal to -1
> removeRow = which(database==-3, arr.ind=T)[,1]
> if(length(removeRow)>0){ 
+     database = database[-removeRow,]  
+ }
> 
> database
     var1 var2 var3
[1,] "A"  "10" "20"
[2,] "B"  "2"  "20"
[3,] "D"  "1"  "20"