[R] If() values in one dataframe then return values from another
Jose Iparraguirre
Jose.Iparraguirre at ageuk.org.uk
Mon Feb 4 19:10:50 CET 2013
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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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.
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.
More information about the R-help
mailing list