[R] Unique in DataFrame
jim holtman
jholtman at gmail.com
Tue Mar 20 17:02:14 CET 2012
This may be what you want:
> x<-data.frame(city="Barcelona",sales=253639)
> x<-rbind(x,data.frame(city="Madrid",sales=223455))
> x<-rbind(x,data.frame(city="Lisbon",sales=273633))
> x<-rbind(x,data.frame(city="Madrid",sales=266535))
> x<-rbind(x,data.frame(city="Barcelona",sales=258369))
> x<-rbind(x,data.frame(city="Lisbon",sales=273633))
> x<-rbind(x,data.frame(city="Barcelona",sales=22579))
> x<-rbind(x,data.frame(city="Lisbon",sales=26333))
> x<-rbind(x,data.frame(city="Barcelona",sales=253639))
>
> x$num<-as.numeric(as.numeric(factor(x$city)))
> View(x)
> x[!duplicated(x$city),]
city sales num
1 Barcelona 253639 1
2 Madrid 223455 2
3 Lisbon 273633 3
>
On Tue, Mar 20, 2012 at 10:16 AM, MSousa <ricardosousa2000 at clix.pt> wrote:
> Hello,
>
> I have little doubt, and I do not think that the way I solve the problem
> is the best way to do it.
> The following is a small dataset
>
>
> x<-data.frame(city="Barcelona",sales=253639)
> x<-rbind(x,data.frame(city="Madrid",sales=223455))
> x<-rbind(x,data.frame(city="Lisbon",sales=273633))
> x<-rbind(x,data.frame(city="Madrid",sales=266535))
> x<-rbind(x,data.frame(city="Barcelona",sales=258369))
> x<-rbind(x,data.frame(city="Lisbon",sales=273633))
> x<-rbind(x,data.frame(city="Barcelona",sales=22579))
> x<-rbind(x,data.frame(city="Lisbon",sales=26333))
> x<-rbind(x,data.frame(city="Barcelona",sales=253639))
>
> x$num<-as.numeric(as.numeric(factor(x$city)))
> View(x)
>
> my problem and my doubts start here, I'm trying to create a list of cities
> and the code that was assigned in.
> x$num<-as.numeric(as.numeric(factor(x$city)))
>
> here seems to work fine, but the largest dataset repeats some values and
> hiding others, this is the correct way to present unique values in a
> column, and view the contents with other columns
>
> rescity<-x[unique(x$city),c(3,1)]
> rescity
>
> Thanks
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Unique-in-DataFrame-tp4488943p4488943.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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.
--
Jim Holtman
Data Munger Guru
What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.
More information about the R-help
mailing list