Here is a suggestion: > Per <- c("NA", "1", "2", "3","4") > NP <- c(1, 1, 2, 1, 1, 2, 2, 1, 4, 1, 0, 5, + 3, 3, 1, 5, 3, 5, 1, 6, 1, 2, 2, 2, + 4, 4, 1, 2, 1, 3, 3, 1, 2, 2, 1, 2, 1, 2, + 2, 3, 1, 1, 4, 4, 1, 1, 1, 2, 2, 2) > Person_CAT <- cut(NP, breaks = c(0:4, Inf)-0.5, labels = Per) > table(Person_CAT) Person_CAT NA 1 2 3 4 1 19 15 6 9 >
You should be aware, though, that items corresponding to the level "NA" will NOT be treated as missing. Bill Venables http://www.cmis.csiro.au/bill.venables/ -----Original Message----- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of PDXRugger Sent: Friday, 31 July 2009 9:54 AM To: r-help@r-project.org Subject: [R] truncating values into separate categories Hi all, Simple question which i thought i had the answer but it isnt so simple for some reason. I am sure someone can easily help. I would like to categorize the values in NP into 1 of the five values in "Per", with the last category("4") representing values >=4(hence 4:max(NP)). The problem is that R is reading max(NP) as multiple values instead of range so the lengths of the labels and the breaks are not matching. Suggestions? Per <- c("NA", "1", "2", "3","4") NP=c(1 ,1 ,2 ,1, 1 ,2 ,2 ,1 ,4 ,1 ,0 ,5 ,3 ,3 ,1 ,5 ,3, 5, 1, 6, 1, 2, 2, 2, 4, 4, 1, 2, 1, 3, 3, 1 ,2 ,2 ,1 ,2, 1, 2, 2, 3, 1, 1, 4, 4, 1, 1, 1, 2, 2, 2) Person_CAT <- cut(NP, breaks=c(0,1,2,3,4:max(NP)), labels=Per) -- View this message in context: http://www.nabble.com/truncating-values-into-separate-categories-tp24749046p24749046.html Sent from the R help mailing list archive at Nabble.com. ______________________________________________ R-help@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. ______________________________________________ R-help@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.