[R] (sans objet)

Gavin Simpson gavin.simpson at ucl.ac.uk
Mon Aug 10 18:48:57 CEST 2009


On Mon, 2009-08-10 at 15:09 +0000, Inchallah Yarab wrote:
> i have written this in R, 
> 
> 
> > data1 <- read.csv2("c:/Total1.csv",sep=",")
> > data2 <- read.csv2("c:/GWPMax1.csv",sep=",")
> > M <- merge(data1, data2, by.x = "Policy.Number", by.y = "Policy.Number")
> > nrow(data1)
> [1] 20
> > nrow(M)
> [1] 12
> > NbOfPolicyWithoutGWPMax <- nrow(data1)-nrow(M)
> > NbOfPolicyWithoutGWPMax
> [1] 8
> > M$GWP_Max <- as.numeric(as.character(M$GWP_Max))
> > m1 <- M[(M[,25]> 0 & M[,25]<1000) ,]
> > nrow(m1)
> [1] 5
> > m2 <- K[(K[,25]>1000 & K[,25]<3000),]
          ^^^^^
Did you mean M? If not, what is K?

> > nrow(m2)
> [1] 3
> > m3 <- K[(K[,25]>3000),]
> > nrow(m3)
> [1] 4
> > 

Those seem to ignore anything that == 1000 or 3000.

> and i want to have in output a table like this 
> GWP_Max NumberOfPolicy 
> withoutGWP 8 
> 0-1000 5 
> 1000-3000 3 
> >3000 4 
> 
> Someone have an idea? 

?cut as in

> ## dummy data
> set.seed(123)
> d <- sample(1:10000, 12)
> ## I'm using d in place of your M[,25]
> tab <- table(cut(d, breaks = c(0,1000,3000,max(d)+1)))
> names(tab) <- c("0-1000", "1000-3000", "3000+")
> tab
   0-1000 1000-3000     3000+ 
        1         1        10 

Note I've not studied ?cut in detail and you'll need to understand how
the end points at the breaks are dealt with, i.e. is the first group
0-1000 or 0 - just less than 1000, with 1000 being considered as part fo
the 1000-3000 group. In particular, see the information about argument
'right' in ?cut.

You can also cut out the need for changing the names(tab) bit by
specifying appropriate labels for each bin using argument 'labels',
again see ?cut.

G

> 
> thank you a lot !!
> 
> 
>       
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> 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.
-- 
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
 Dr. Gavin Simpson             [t] +44 (0)20 7679 0522
 ECRC, UCL Geography,          [f] +44 (0)20 7679 0565
 Pearson Building,             [e] gavin.simpsonATNOSPAMucl.ac.uk
 Gower Street, London          [w] http://www.ucl.ac.uk/~ucfagls/
 UK. WC1E 6BT.                 [w] http://www.freshwaters.org.uk
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%




More information about the R-help mailing list