Hello everybody, I am new to R and I have a very basic question, but I couldn't get this to work. Let's say I have a vector s = c("a","a","a","b","b","c","c","c","c") s1 <- factor(s) s2 <- summary(s1) leads to the following a b c 3 2 4 How can I access the different aggregated values for a b and c? I am not quite sure if the factor method is the right approach. I tried to use s2$a but it didn't work. Any suggestions? Thanks a lot for your help Marc
Hi, try ?table # for example (s3 <- table(s)) # and if you want a single value s3["a"] # or s3[1] HTH, Colin. ________________________________ From: r-help-bounces@r-project.org on behalf of Marc Giombetti Sent: Tue 17/11/2009 22:55 To: r-help@r-project.org Subject: [R] Basic question on nominal data Hello everybody, I am new to R and I have a very basic question, but I couldn't get this to work. Let's say I have a vector s = c("a","a","a","b","b","c","c","c","c") s1 <- factor(s) s2 <- summary(s1) leads to the following a b c 3 2 4 How can I access the different aggregated values for a b and c? I am not quite sure if the factor method is the right approach. I tried to use s2$a but it didn't work. Any suggestions? Thanks a lot for your help Marc ______________________________________________ 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. ______________________________________________________________________ This email has been scanned by the MessageLabs Email Security System. For more information please visit http://www.messagelabs.com/email ______________________________________________________________________ [[alternative HTML version deleted]]
On 17-Nov-09 22:55:32, Marc Giombetti wrote:> Hello everybody, > I am new to R and I have a very basic question, but I couldn't get > this to work. > Let's say I have a vector > > s = c("a","a","a","b","b","c","c","c","c") > s1 <- factor(s) > > s2 <- summary(s1) leads to the following > a b c > 3 2 4 > > How can I access the different aggregated values for a b and c? I am > not quite sure if the factor method is the right approach. > I tried to use s2$a but it didn't work. > > Any suggestions? > > Thanks a lot for your help > Marcs2$a doesn't work because s2 is not a list. It is a vector. You can find this out with str(s2) # Named int [1:3] 3 2 4 # - attr(*, "names")= chr [1:3] "a" "b" "c" so you can access its elements in the usual way; also by exploiting its "names" attribute: s2[1] # a # 3 s2["a"] # a # 3 s2[2] # b # 2 s2["b"] # b # 2 etc. Ted. -------------------------------------------------------------------- E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk> Fax-to-email: +44 (0)870 094 0861 Date: 17-Nov-09 Time: 23:26:25 ------------------------------ XFMail ------------------------------