Spartacus Rocha
Spartacus Rocha

Reputation: 606

R removing data frame row according to a vector value

I need to remove a row from a data frame accordingly to a value from another data frame. I believe I am close doing something dumb on my formula, but I simply can't see what it is so I ask for your help.

I have this data frame, from where I want to remove some lines:

> Teste
   Q1 Q2 Q3 Q4 Q5 Q6 Q7 Q8 Q9 Q10 Q11 Q12 Q13 Q14 Q15 Q16 Q17 Q18 Q19 Q20 Q21
1   5  4  4  4  6  6  7  6  4   6   6   6   4   4   4   4   6   7   4   4   6
2   5  4  4  5  5  5  5  5  5   5   7   5   4   3   1   6   6   5   6   7   7
3   4  5  4  4  5  4  5  4  5   4   5   4   5   4   5   4   5   4   5   4   5
4   5  5  7  7  4  6  6  6  7   7   6   7   7   6   6   7   4   7   6   6   7
5   6  1  7  6  7  7  7  7  7   7   6   7   2   2   3   6   3   7   7   7   7
6   4  4  3  3  4  5  4  3  4   7   6   6   4   4   6   4   5   7   6   6   7
7   1  1  7  7  7  3  7  6  2   7   6   7   6   7   4   7   7   7   7   4   7
8   5  5  6  7  6  6  4  4  5   6   7   6   4   4   5   3   3   7   6   6   7
9   5  4  7  4  7  6  6  7  5   7   6   7   4   5   4   7   5   7   7   7   7
10  6  4  7  4  7  6  6  7  5   7   7   7   4   4   3   6   4   7   7   6   7
11  7  7  7  7  7  7  7  7  7   7   7   7   7   7   7   7   7   7   7   7   7

> dput(Teste)
structure(list(Q1 = c(5L, 5L, 4L, 5L, 6L, 4L, 1L, 5L, 5L, 6L, 
7L), Q2 = c(4L, 4L, 5L, 5L, 1L, 4L, 1L, 5L, 4L, 4L, 7L), Q3 = c(4L, 
4L, 4L, 7L, 7L, 3L, 7L, 6L, 7L, 7L, 7L), Q4 = c(4L, 5L, 4L, 7L, 
6L, 3L, 7L, 7L, 4L, 4L, 7L), Q5 = c(6L, 5L, 5L, 4L, 7L, 4L, 7L, 
6L, 7L, 7L, 7L), Q6 = c(6L, 5L, 4L, 6L, 7L, 5L, 3L, 6L, 6L, 6L, 
7L), Q7 = c(7L, 5L, 5L, 6L, 7L, 4L, 7L, 4L, 6L, 6L, 7L), Q8 = c(6L, 
5L, 4L, 6L, 7L, 3L, 6L, 4L, 7L, 7L, 7L), Q9 = c(4L, 5L, 5L, 7L, 
7L, 4L, 2L, 5L, 5L, 5L, 7L), Q10 = c(6L, 5L, 4L, 7L, 7L, 7L, 
7L, 6L, 7L, 7L, 7L), Q11 = c(6L, 7L, 5L, 6L, 6L, 6L, 6L, 7L, 
6L, 7L, 7L), Q12 = c(6L, 5L, 4L, 7L, 7L, 6L, 7L, 6L, 7L, 7L, 
7L), Q13 = c(4L, 4L, 5L, 7L, 2L, 4L, 6L, 4L, 4L, 4L, 7L), Q14 = c(4L, 
3L, 4L, 6L, 2L, 4L, 7L, 4L, 5L, 4L, 7L), Q15 = c(4L, 1L, 5L, 
6L, 3L, 6L, 4L, 5L, 4L, 3L, 7L), Q16 = c(4L, 6L, 4L, 7L, 6L, 
4L, 7L, 3L, 7L, 6L, 7L), Q17 = c(6L, 6L, 5L, 4L, 3L, 5L, 7L, 
3L, 5L, 4L, 7L), Q18 = c(7L, 5L, 4L, 7L, 7L, 7L, 7L, 7L, 7L, 
7L, 7L), Q19 = c(4L, 6L, 5L, 6L, 7L, 6L, 7L, 6L, 7L, 7L, 7L), 
Q20 = c(4L, 7L, 4L, 6L, 7L, 6L, 4L, 6L, 7L, 6L, 7L), Q21 = c(6L, 
7L, 5L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L)), .Names = c("Q1", 
"Q2", "Q3", "Q4", "Q5", "Q6", "Q7", "Q8", "Q9", "Q10", "Q11", 
"Q12", "Q13", "Q14", "Q15", "Q16", "Q17", "Q18", "Q19", "Q20", 
"Q21"), row.names = c(NA, 11L), class = "data.frame")

My criteria is to remove rows accordingly to values from this vector:

> Teste2
   apply.TiposMotivA.1.11.....1..function.x..sum.x....Mode.x...
1                                                            10
2                                                            10
3                                                            11
4                                                             9
5                                                            12
6                                                             8
7                                                            12
8                                                             7
9                                                            10
10                                                            9
11                                                           21
> dput(Teste2)
structure(list(apply.TiposMotivA.1.11.....1..function.x..sum.x....Mode.x... = c(10L, 
10L, 11L, 9L, 12L, 8L, 12L, 7L, 10L, 9L, 21L)), .Names = "apply.TiposMotivA.1.11.....1..function.x..sum.x....Mode.x...", row.names = c ("1", 
"2", "3", "4", "5", "6", "7", "8", "9", "10", "11"), class = "data.frame")

Any row in Teste where the corresponding value in Teste2 divided by 0.21 is smaller than 0.762 must be removed. I am trying to reach this by using this command, but I am getting the following error:

> Teste3 <- Teste[!apply(Teste2,1,function(x) {any((x / 0.21) >= 0.762)}),]
Error in apply(Teste2, 1, function(x) { : 
dim(X) must have a positive length

Any help will be very appreciated!

Upvotes: 1

Views: 63

Answers (1)

Marat Talipov
Marat Talipov

Reputation: 13304

Perhaps I'm missing something, but it seems to me that you need simply:

Teste[Teste2/.21 < .762,]

If you'd like Teste2 to be a data frame:

Teste[Teste2[,1]/.21 < .762,]

Upvotes: 2

Related Questions