Steven Ranney
2013-Feb-04 17:48 UTC
[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 [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) 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
David Winsemius
2013-Feb-04 18:08 UTC
[R] 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,]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 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 9The 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
Jose Iparraguirre
2013-Feb-04 18:10 UTC
[R] If() values in one dataframe then return values from another
Hi Steven, You are not indexing mA properly within the loop. In order not to change the elements of the object, I'll create a new vector, mA2 -not strictly necessary, though. Let's start with an empty vector:> mA2 <- rep(0,40)Now, the loop with the correct indexing:> for(i in 1:40){+ if( (any(data1[i,] == 3)) ) mA2[i] <- mA$meter[i] else mA2[i] <- NA } You'll get:> mA2[1] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA [26] NA NA NA NA NA NA NA 1 NA NA NA NA NA NA 15 Hope this helps, Jos? Jos? Iparraguirre Chief Economist Age UK -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Steven Ranney Sent: 04 February 2013 17:48 To: r-help at r-project.org 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 [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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 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. Wrap Up and Run 10k is back! Also, new for 2013 ? 2km intergenerational walks at selected venues. So recruit a buddy, dust off the trainers and beat the winter blues by signing up now: http://www.ageuk.org.uk/10k Milton Keynes | Oxford | Sheffield | Crystal Palace | Exeter | Harewood House, Leeds | Tatton Park, Cheshire | Southampton | Coventry Age UK Improving later life http://www.ageuk.org.uk ------------------------------- Age UK is a registered charity and company limited by guarantee, (registered charity number 1128267, registered company number 6825798). Registered office: Tavis House, 1-6 Tavistock Square, London WC1H 9NA. For the purposes of promoting Age UK Insurance, Age UK is an Appointed Representative of Age UK Enterprises Limited, Age UK is an Introducer Appointed Representative of JLT Benefit Solutions Limited and Simplyhealth Access for the purposes of introducing potential annuity and health cash plans customers respectively. Age UK Enterprises Limited, JLT Benefit Solutions Limited and Simplyhealth Access are all authorised and regulated by the Financial Services Authority. ------------------------------ This email and any files transmitted with it are confidential and intended solely for the use of the individual or entity to whom they are addressed. If you receive a message in error, please advise the sender and delete immediately. Except where this email is sent in the usual course of our business, any opinions expressed in this email are those of the author and do not necessarily reflect the opinions of Age UK or its subsidiaries and associated companies. Age UK monitors all e-mail transmissions passing through its network and may block or modify mails which are deemed to be unsuitable. Age Concern England (charity number 261794) and Help the Aged (charity number 272786) and their trading and other associated companies merged on 1st April 2009. Together they have formed the Age UK Group, dedicated to improving the lives of people in later life. The three national Age Concerns in Scotland, Northern Ireland and Wales have also merged with Help the Aged in these nations to form three registered charities: Age Scotland, Age NI, Age Cymru.
Rui Barradas
2013-Feb-04 18:19 UTC
[R] If() values in one dataframe then return values from another
Hello, Try the following. mA <- sapply(seq_len(nrow(data1)), function(i) if(any(data1[i,] == 3)) meter[i, 1] else NA) Hope this helps, Rui Barradas Em 04-02-2013 17:48, Steven Ranney escreveu:> 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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 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. >
arun
2013-Feb-04 18:26 UTC
[R] If() values in one dataframe then return values from another
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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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 ? [10,]? 1? 1? 1? 1? 1? 1? 1? 1? 1? 1? 1? 1? 1? 1? 1? 1? 1? 2?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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.