[R] If() values in one dataframe then return values from another
arun
smartpink111 at yahoo.com
Mon Feb 4 19:26:16 CET 2013
Hi,
I am not sure about what your end result should be:
From your code, it looks like you want to replace the rows of dat1 that contain at least a `3` with the corresponding row of dat2. Or is it only the cell with number `3` replaced by corresponding row in dat2.
dat1<- read.table(text="
A1 A2 A3 A4 A5 A6 A7 A8 A9 A10 A11 A12 A13 A14 A15 A16 A17 A18 A19 A20
0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0
0 0 0 0 0 1 0 0 1 0 1 0 0 1 0 0 0 0 0 0
1 1 0 1 1 1 1 1 1 1 1 0 1 1 0 0 0 0 0 0
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 0 0 2
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 2 0 2
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 2 2 2
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 2 2 2
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 2 0 0 0 0 0 0 0 2 0 0 0 1 0 0 0 0
0 0 0 2 0 0 0 1 0 0 0 2 0 0 0 2 0 0 0 2
0 0 1 2 0 0 0 2 0 0 1 1 0 1 0 1 0 0 2 2
0 1 1 2 0 2 2 2 1 1 1 1 0 1 1 1 1 0 2 2
1 1 1 1 1 2 2 2 1 1 1 1 2 1 1 1 1 2 2 2
1 1 1 1 1 2 1 2 1 1 1 1 2 1 1 1 1 2 2 2
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2
1 1 1 1 1 1 1 2 1 1 1 1 1 1 2 1 1 2 2 2
1 1 1 2 1 1 1 2 1 1 1 1 1 1 1 2 1 2 2 2
1 1 2 1 1 1 1 1 1 1 1 1 2 2 1 1 1 2 2 2
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2
1 1 1 1 1 3 3 3 3 1 3 3 1 1 2 1 1 2 2 2
1 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 2 2 2
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2
0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 2 0 0 2 0 0 2 0 0 0 2
1 1 2 1 1 2 1 2 2 2 2 2 2 1 1 1 1 2 3 2
",sep="",header=TRUE)
dat2<- read.table(text="
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
",sep="",header=TRUE)
res<-do.call(rbind,lapply(1:nrow(dat1),function(i) rep(ifelse(any(dat1[i,]==3),dat2[i,],NA),ncol(dat1))))
tail(res)
# [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13]
#[35,] NA NA NA NA NA NA NA NA NA NA NA NA NA
#[36,] NA NA NA NA NA NA NA NA NA NA NA NA NA
#[37,] NA NA NA NA NA NA NA NA NA NA NA NA NA
#[38,] NA NA NA NA NA NA NA NA NA NA NA NA NA
#[39,] NA NA NA NA NA NA NA NA NA NA NA NA NA
#[40,] 15 15 15 15 15 15 15 15 15 15 15 15 15
# [,14] [,15] [,16] [,17] [,18] [,19] [,20]
#[35,] NA NA NA NA NA NA NA
#[36,] NA NA NA NA NA NA NA
#[37,] NA NA NA NA NA NA NA
#[38,] NA NA NA NA NA NA NA
#[39,] NA NA NA NA NA NA NA
#[40,] 15 15 15 15 15 15 15
A.K.
----- Original Message -----
From: Steven Ranney <steven.ranney at gmail.com>
To: r-help at r-project.org
Cc:
Sent: Monday, February 4, 2013 12:48 PM
Subject: [R] If() values in one dataframe then return values from another
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
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 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)
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( (any(data1[i,] == 3)) ) mA <- meter[i,] else mA <- 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
Can anyone give me some additional ideas to try?
Thanks -
SR
Steven H. Ranney
______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
More information about the R-help
mailing list