If() values in one dataframe then return values from another
On Feb 4, 2013, at 9:48 AM, Steven Ranney wrote:
I have a large data frame ("data1") that looks like:
A1 A2 A3 A4 A5 A6 A7 A8 A9 A10 A11 A12 A13 A14 A15 A16 A17 A18 A19 A20
[1,] 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0
[2,] 0 0 0 0 0 1 0 0 1 0 1 0 0 1 0 0 0 0 0 0
[3,] 1 1 0 1 1 1 1 1 1 1 1 0 1 1 0 0 0 0 0 0
[4,] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 0 0 2
[5,] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 2 0 2
[6,] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 2 2 2
[7,] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2
[8,] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2
[9,] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2
[10,] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2
[11,] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2
[12,] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2
[13,] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2
[14,] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2
[15,] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2
[16,] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2
[17,] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2
[18,] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2
[19,] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 2 2 2
[20,] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2
[21,] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
[22,] 0 0 0 2 0 0 0 0 0 0 0 2 0 0 0 1 0 0 0 0
[23,] 0 0 0 2 0 0 0 1 0 0 0 2 0 0 0 2 0 0 0 2
[24,] 0 0 1 2 0 0 0 2 0 0 1 1 0 1 0 1 0 0 2 2
[25,] 0 1 1 2 0 2 2 2 1 1 1 1 0 1 1 1 1 0 2 2
[26,] 1 1 1 1 1 2 2 2 1 1 1 1 2 1 1 1 1 2 2 2
[27,] 1 1 1 1 1 2 1 2 1 1 1 1 2 1 1 1 1 2 2 2
[28,] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2
[29,] 1 1 1 1 1 1 1 2 1 1 1 1 1 1 2 1 1 2 2 2
[30,] 1 1 1 2 1 1 1 2 1 1 1 1 1 1 1 2 1 2 2 2
[31,] 1 1 2 1 1 1 1 1 1 1 1 1 2 2 1 1 1 2 2 2
[32,] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2
[33,] 1 1 1 1 1 3 3 3 3 1 3 3 1 1 2 1 1 2 2 2
[34,] 1 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 2 2 2
[35,] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2
[36,] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2
[37,] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2
[38,] 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0
[39,] 0 0 0 0 0 0 0 0 0 2 0 0 2 0 0 2 0 0 0 2
[40,] 1 1 2 1 1 2 1 2 2 2 2 2 2 1 1 1 1 2 3 2
(data1 is actually 1,080 rows long.)
If there is a "3" in any row above, I'd like to be given the values
from a second data frame ("meter"):
meter
1 20
2 19
3 18
4 17
5 16
6 15
7 14
8 13
9 12
10 11
11 10
12 9
13 8
14 7
15 6
16 5
17 4
18 3
19 2
20 1
21 20
22 19
23 18
24 17
25 16
26 15
27 14
28 13
29 8
30 7
31 6
32 2
33 1
34 10
35 4
36 3
37 2
38 18
39 17
40 15
(meter is actually 1,080 rows long)
One hopes for the success of htis effort that 'data1' has an equal number of rows.
If there is no "3" row i of data1, I'd like to R to return "NA".
I've tried
for(i in 1:40){
if(
}
Generally sequential testing is best handled with `ifelse` rather than with for loops and `if () {} else{}`
mA <- ifelse ( apply(data, 1, any), meter, NA)
But I'm given:
mA
[1] 15 Levels: 1 10 11 12 13 14 15 16 17 18 19 2 20 3 4 5 6 7 8 9
The fact that you have constructed a factor variable suggest that you made an error in building the 'meter' data and that is it no numeric at the moment. The fact that it is only length = 1 is due to your not indexing the construction of 'mA' in your loop.
Can anyone give me some additional ideas to try? Thanks -
David Winsemius Alameda, CA, USA