Hi.
I have a factor and I want to extract just those elements that appear
exactly once.
How to do this?
Toy example follows.
> a <- as.factor(c(rep("oak",5) ,rep("ash",1),rep("elm",1),rep
("beech",4)))
> a
[1] oak oak oak oak oak ash elm beech beech beech beech
Levels: ash beech elm oak
> table(a)
a
ash beech elm oak
1 4 1 5
>
So I would want "ash" and "elm", because there is only one ash and
only one elm in my wood.
My Best Effort:
> names(table(a)[table(a)==1])
[1] "ash" "elm"
>
This doesn't seem particularly elegant to me; there must be a better
way!
anyone?
--
Robin Hankin
Uncertainty Analyst
National Oceanography Centre, Southampton
European Way, Southampton SO14 3ZH, UK
tel 023-8059-7743
elements that appear only once
7 messages · robin hankin, Dimitris Rizopoulos, Adaikalavan Ramasamy +3 more
another approach is: names(which(table(a) == 1)) but I don't know if you find this more elegant :) Best, Dimitris ---- Dimitris Rizopoulos Ph.D. Student Biostatistical Centre School of Public Health Catholic University of Leuven Address: Kapucijnenvoer 35, Leuven, Belgium Tel: +32/(0)16/336899 Fax: +32/(0)16/337015 Web: http://www.med.kuleuven.be/biostat/ http://www.student.kuleuven.be/~m0390867/dimitris.htm ----- Original Message ----- From: "Robin Hankin" <r.hankin at noc.soton.ac.uk> To: "RHelp" <r-help at stat.math.ethz.ch> Sent: Wednesday, February 22, 2006 10:11 AM Subject: [R] elements that appear only once
Hi. I have a factor and I want to extract just those elements that appear exactly once. How to do this? Toy example follows.
a <- as.factor(c(rep("oak",5) ,rep("ash",1),rep("elm",1),rep
("beech",4)))
a
[1] oak oak oak oak oak ash elm beech beech beech beech Levels: ash beech elm oak
table(a)
a
ash beech elm oak
1 4 1 5
So I would want "ash" and "elm", because there is only one ash and only one elm in my wood. My Best Effort:
names(table(a)[table(a)==1])
[1] "ash" "elm"
This doesn't seem particularly elegant to me; there must be a better way! anyone? -- Robin Hankin Uncertainty Analyst National Oceanography Centre, Southampton European Way, Southampton SO14 3ZH, UK tel 023-8059-7743
______________________________________________ R-help at stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm
Hi Dmitris, and list
On 22 Feb 2006, at 09:24, Dimitris Rizopoulos wrote:
another approach is: names(which(table(a) == 1)) but I don't know if you find this more elegant :)
well, thank you for this (which() is good here!) but this is still "inelegant" IMHO because it uses the names() of a table. If I had > a <- as.factor(c(1,1,1,2,3,4,4,4,4,5)) > names(which(table(a)==1)) [1] "2" "3" "5" > this gives a character vector. I could coerce using as.integer() here, but this seems so....inelegant. best wishes Robin
Best, Dimitris
Hi. I have a factor and I want to extract just those elements that appear exactly once. How to do this? Toy example follows.
a <- as.factor(c(rep("oak",5) ,rep("ash",1),rep("elm",1),rep
("beech",4)))
a
[1] oak oak oak oak oak ash elm beech beech beech beech Levels: ash beech elm oak
table(a)
a
ash beech elm oak
1 4 1 5
So I would want "ash" and "elm", because there is only one ash and only one elm in my wood. My Best Effort:
names(table(a)[table(a)==1])
[1] "ash" "elm"
This doesn't seem particularly elegant to me; there must be a better way! anyone?
-- Robin Hankin Uncertainty Analyst National Oceanography Centre, Southampton European Way, Southampton SO14 3ZH, UK tel 023-8059-7743
A slight variation on your solution but hopefully more readable: names( which( table(a) == 1 ) ) Regards, Adai
On Wed, 2006-02-22 at 09:11 +0000, Robin Hankin wrote:
Hi. I have a factor and I want to extract just those elements that appear exactly once. How to do this? Toy example follows.
> a <- as.factor(c(rep("oak",5) ,rep("ash",1),rep("elm",1),rep
("beech",4)))
> a
[1] oak oak oak oak oak ash elm beech beech beech beech Levels: ash beech elm oak
> table(a)
a
ash beech elm oak
1 4 1 5
>
So I would want "ash" and "elm", because there is only one ash and only one elm in my wood. My Best Effort:
> names(table(a)[table(a)==1])
[1] "ash" "elm"
>
This doesn't seem particularly elegant to me; there must be a better way! anyone? -- Robin Hankin Uncertainty Analyst National Oceanography Centre, Southampton European Way, Southampton SO14 3ZH, UK tel 023-8059-7743
______________________________________________ R-help at stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
On Wed, 22 Feb 2006, Robin Hankin wrote:
Hi Dmitris, and list On 22 Feb 2006, at 09:24, Dimitris Rizopoulos wrote:
another approach is: names(which(table(a) == 1)) but I don't know if you find this more elegant :)
Since the names of the table are the levels of the factor, I would use levels(a)[table(a) %in% 1] Well, almost. If you have NA or NaN as a factor level then all these solutions need to be more complicated.
well, thank you for this (which() is good here!) but this is still "inelegant" IMHO because it uses the names() of a table. If I had
a <- as.factor(c(1,1,1,2,3,4,4,4,4,5)) names(which(table(a)==1))
[1] "2" "3" "5"
this gives a character vector. I could coerce using as.integer() here, but this seems so....inelegant.
But a has a character vector of levels, and there is nothing there to tell R that you wanted integers and not decimal-digit character strings.
Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
I am not sure whether this is desirable but here is another way just in case: paste(setdiff(a, a[duplicated(a)])) You could replace paste with as.character if you prefer or could remove it entirely if you want the result as a factor.
On 2/22/06, Robin Hankin <r.hankin at noc.soton.ac.uk> wrote:
Hi. I have a factor and I want to extract just those elements that appear exactly once. How to do this? Toy example follows.
> a <- as.factor(c(rep("oak",5) ,rep("ash",1),rep("elm",1),rep
("beech",4)))
> a
[1] oak oak oak oak oak ash elm beech beech beech beech Levels: ash beech elm oak
> table(a)
a
ash beech elm oak
1 4 1 5
>
So I would want "ash" and "elm", because there is only one ash and only one elm in my wood. My Best Effort:
> names(table(a)[table(a)==1])
[1] "ash" "elm"
>
This doesn't seem particularly elegant to me; there must be a better way! anyone? -- Robin Hankin Uncertainty Analyst National Oceanography Centre, Southampton European Way, Southampton SO14 3ZH, UK tel 023-8059-7743
______________________________________________ R-help at stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Robin Hankin <r.hankin at noc.soton.ac.uk> writes:
Hi Dmitris, and list On 22 Feb 2006, at 09:24, Dimitris Rizopoulos wrote:
another approach is: names(which(table(a) == 1)) but I don't know if you find this more elegant :)
well, thank you for this (which() is good here!) but this is still "inelegant" IMHO because it uses the names() of a table. If I had
> a <- as.factor(c(1,1,1,2,3,4,4,4,4,5)) > names(which(table(a)==1))
[1] "2" "3" "5"
>
this gives a character vector. I could coerce using as.integer() here, but this seems so....inelegant.
You could go sort(unique(a))[table(a)==1] (without the sort(), things go pearshaped, try setting a <- factor(c(1,1,1,2,3,4,4,4,4,5),levels=5:1) wnd you'll see). If we assume that a is a factor, another option is levels(a))[table(a)==1] but that also has the problem of returning a character vector.
O__ ---- Peter Dalgaard ??ster Farimagsgade 5, Entr.B c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907