[R] Truncating based on attribute range and serial no
Marc Schwartz
marc_schwartz at me.com
Tue Aug 4 00:28:07 CEST 2009
On Aug 3, 2009, at 2:04 PM, PDXRugger wrote:
>
> COnsider the following:
>
> Age<-c(48, 57, 56, 76, 76, 66, 70, 14, 7, 3, 62, 62, 30, 10, 7,
> 53, 44,
> 29, 46, 47, 15, 13, 84, 77, 26)
>
> SerialNo<-c(001147, 005979, 005979, 006128, 006128, 007004, 007004,
> 007004,
> 007004, 007004, 007438, 007438,009402,009402, 009402, 012693, 012693,
> 012693, 014063,014063, 014063, 014063, 014811, 014811,016570)
>
> TestSet<-cbind(Age,SerialNo)
>
> TestSet<-data.frame(TestSet)
>
> I am looking to create a third column titled "IsHead". This column
> would be
> either TRUE or FALSE depending on whether the Age variable was the
> greatest
> for that set of SerialNo's. So for the above i would return:
>
> Age SerialNo IsHead
> 48 1147 TRUE
> 57 5979 TRUE
> 56 5979 FALSE
> 76 6128 TRUE
> 76 6128 FALSE <--- TRUE for both 76?
> 66 7004 FALSE
> 70 7004 TRUE
> 14 7004 FALSE
> 7 7004 FALSE
> 3 7004 FALSE
>
> I am thinking this is simple but cannot get my own code to work.
> Thanks for
> any insights.
Try this:
> TestSet
Age SerialNo
1 48 1147
2 57 5979
3 56 5979
4 76 6128
5 76 6128
6 66 7004
7 70 7004
8 14 7004
9 7 7004
10 3 7004
11 62 7438
12 62 7438
13 30 9402
14 10 9402
15 7 9402
16 53 12693
17 44 12693
18 29 12693
19 46 14063
20 47 14063
21 15 14063
22 13 14063
23 84 14811
24 77 14811
25 26 16570
# See ?ave for more information
# As implemented, this will work for integer values of Age
TestSet$IsHead <- as.logical(ave(TestSet$Age,
TestSet$SerialNo,
FUN = function(x) x == max(x)))
> TestSet
Age SerialNo IsHead
1 48 1147 TRUE
2 57 5979 TRUE
3 56 5979 FALSE
4 76 6128 TRUE
5 76 6128 TRUE
6 66 7004 FALSE
7 70 7004 TRUE
8 14 7004 FALSE
9 7 7004 FALSE
10 3 7004 FALSE
11 62 7438 TRUE
12 62 7438 TRUE
13 30 9402 TRUE
14 10 9402 FALSE
15 7 9402 FALSE
16 53 12693 TRUE
17 44 12693 FALSE
18 29 12693 FALSE
19 46 14063 FALSE
20 47 14063 TRUE
21 15 14063 FALSE
22 13 14063 FALSE
23 84 14811 TRUE
24 77 14811 FALSE
25 26 16570 TRUE
HTH,
Marc Schwartz
More information about the R-help
mailing list